recording2.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <view>
  3. <view>
  4. <view class="record-btn-layer">
  5. <button
  6. class="record-btn"
  7. :class="longPress == '1' ? 'record-btn-1' : 'record-btn-2'"
  8. :style="
  9. VoiceTitle != '松开手指,取消发送' && longPress != '1'
  10. ? 'background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);'
  11. : 'background-color: rgba(0, 0, 0, .5);color:white'
  12. "
  13. @longtap="longpressBtn"
  14. @touchend="touchendBtn()"
  15. @touchmove="handleTouchMove"
  16. @touchstart="longpressBtn"
  17. >
  18. <text>{{ VoiceText }}</text>
  19. </button>
  20. </view>
  21. <!-- 语音音阶动画 -->
  22. <view :class="VoiceTitle != '松开手指,取消发送' ? 'prompt-layer prompt-layer-1' : 'prompt-layer1 prompt-layer-1'" v-if="longPress == '2'">
  23. <view class="prompt-loader">
  24. <view class="em" v-for="(item, index) in 15" :key="index"></view>
  25. </view>
  26. <text class="span">{{ VoiceTitle }}</text>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. const recorderManager = uni.getRecorderManager()
  33. var init // 录制时长计时器
  34. var timer // 播放 录制倒计时
  35. export default {
  36. data() {
  37. return {
  38. longPress: '1', // 1显示 按住 说话 2显示 说话中
  39. delShow: false, // 删除提示框显示隐藏
  40. time: 0, //录音时长
  41. duration: 60000, //录音最大值ms 60000/1分钟
  42. tempFilePath: '', //音频路径
  43. startPoint: {}, //记录长按录音开始点信息,用于后面计算滑动距离。
  44. sendLock: true, //发送锁,当为true时上锁,false时解锁发送
  45. VoiceTitle: '松手结束录音',
  46. recorderManager: uni.getRecorderManager(),
  47. VoiceText: '按住 说话',
  48. types: ''
  49. }
  50. },
  51. props: ['type'],
  52. created: function () {
  53. this.types = this.type
  54. console.log(this.type) //打印父组件传过来的参数
  55. },
  56. methods: {
  57. // 长按录音事件
  58. longpressBtn(e) {
  59. this.startPoint = e.touches[0] //记录长按时开始点信息,后面用于计算上划取消时手指滑动的距离。
  60. this.longPress = '2'
  61. this.VoiceText = '说话中...'
  62. recorderManager.onStop((res) => {
  63. // console.log(res);
  64. this.tempFilePath = res.tempFilePath
  65. })
  66. const options = {
  67. duration: this.duration, // 指定录音的时长,单位 ms
  68. sampleRate: 16000, // 采样率
  69. numberOfChannels: 1, // 录音通道数
  70. encodeBitRate: 96000, // 编码码率
  71. format: 'mp3', // 音频格式,有效值 aac/mp3
  72. frameSize: 10 // 指定帧大小,单位 KB
  73. }
  74. this.recorderManager.start(options)
  75. // 监听音频开始事件
  76. this.sendLock = false //长按时是不上锁的。
  77. recorderManager.onStart((res) => {})
  78. },
  79. // 长按松开录音事件
  80. touchendBtn() {
  81. this.longPress = '1'
  82. this.VoiceText = '按住 说话'
  83. this.VoiceTitle = '松手结束录音'
  84. recorderManager.onStop((res) => {
  85. console.log(this.sendLock)
  86. if (this.sendLock) {
  87. //上锁不发送
  88. } else {
  89. //解锁发送,发送网络请求
  90. if (res.duration < 1000)
  91. wx.showToast({
  92. title: '录音时间太短',
  93. icon: 'none',
  94. duration: 1000
  95. })
  96. else {
  97. this.tempFilePath = res.tempFilePath
  98. console.log(this.tempFilePath + '666')
  99. // uploadFile({
  100. // url: '/voice/VoiceControl',
  101. // src: res.tempFilePath,
  102. // }).then(res => {
  103. // console.log(JSON.parse(res.data));
  104. // })
  105. }
  106. }
  107. })
  108. // this.recordingTimer(this.time)
  109. this.recorderManager.stop() //结束录音
  110. },
  111. // 删除录音
  112. handleTouchMove(e) {
  113. //touchmove时触发
  114. var moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY //移动距离
  115. if (Math.abs(moveLenght) > 70) {
  116. // wx.showToast({
  117. // title: "松开手指,取消发送",
  118. // icon: "none",
  119. // duration: 60000
  120. // });
  121. // console.log("松开手指,取消发送");
  122. this.VoiceTitle = '松开手指,取消发送'
  123. this.VoiceText = '松开手指,取消发送'
  124. this.delBtn()
  125. this.sendLock = true //触发了上滑取消发送,上锁
  126. } else {
  127. // wx.showToast({
  128. // title: "正在录音,上划取消发送",
  129. // icon: "none",
  130. // duration: 60000
  131. // });
  132. this.VoiceTitle = '松手结束录音'
  133. this.VoiceText = '松手结束录音'
  134. this.sendLock = false //上划距离不足,依然可以发送,不上锁
  135. }
  136. },
  137. delBtn() {
  138. this.delShow = false
  139. this.time = 0
  140. this.tempFilePath = ''
  141. // this.VoiceTitle = '松手结束录音'
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss">
  147. /* 语音录制开始--------------------------------------------------------------------- */
  148. // .record-layer {
  149. // width: 91vw;
  150. // padding: 300px 0;
  151. // box-sizing: border-box;
  152. // height: 15vw;
  153. // position: fixed;
  154. // margin-left: 4vw;
  155. // z-index: 10;
  156. // bottom: 3vh;
  157. // }
  158. // .record-layer1 {
  159. // width: 100vw;
  160. // padding: 300px 0;
  161. // box-sizing: border-box;
  162. // height: 100vh;
  163. // position: fixed;
  164. // background-color: rgba(0, 0, 0, 0.6);
  165. // // padding: 0 4vw;
  166. // z-index: 10;
  167. // bottom: 0vh;
  168. // }
  169. // .record-box {
  170. // width: 100%;
  171. // position: relative;
  172. // }
  173. // .record-box1 {
  174. // width: 100%;
  175. // position: relative;
  176. // bottom: -83vh;
  177. // height: 17vh;
  178. // }
  179. .record-btn-layer {
  180. width: 100%;
  181. }
  182. .record-btn-layer button::after {
  183. border: none;
  184. transition: all 0.1s;
  185. }
  186. .record-btn-layer button {
  187. margin: auto;
  188. font-size: 14px;
  189. line-height: 50px;
  190. width: 50%;
  191. height: 50px;
  192. border-radius: 8px;
  193. text-align: center;
  194. background: #ffd300;
  195. transition: all 0.1s;
  196. }
  197. .record-btn-layer .record-btn-1 {
  198. background-image: linear-gradient(to right, #43e97b 0%, #38f9d7 100%);
  199. color: #000000 !important;
  200. }
  201. .record-btn-layer .record-btn-2 {
  202. border-radius: 168rpx 168rpx 0 0;
  203. height: 17vh;
  204. line-height: 17vh;
  205. transition: all 0.3s;
  206. }
  207. /* 提示小弹窗 */
  208. .prompt-layer {
  209. border-radius: 15px;
  210. background: #95eb6c;
  211. padding: 8px 16px;
  212. box-sizing: border-box;
  213. position: absolute;
  214. left: 50%;
  215. height: 11vh;
  216. transform: translateX(-50%);
  217. transition: all 0.3s;
  218. }
  219. .prompt-layer::after {
  220. content: '';
  221. display: block;
  222. border: 12px solid rgba(0, 0, 0, 0);
  223. border-radius: 10rpx;
  224. border-top-color: #95eb6c;
  225. position: absolute;
  226. bottom: -46rpx;
  227. left: 50%;
  228. transform: translateX(-50%);
  229. transition: all 0.3s;
  230. }
  231. //取消动画
  232. .prompt-layer1 {
  233. border-radius: 15px;
  234. background: #fb5353;
  235. padding: 8px 16px;
  236. box-sizing: border-box;
  237. position: absolute;
  238. left: 50%;
  239. height: 11vh;
  240. transform: translateX(-50%);
  241. transition: all 0.3s;
  242. }
  243. .prompt-layer1::after {
  244. content: '';
  245. display: block;
  246. border: 12px solid rgba(0, 0, 0, 0);
  247. border-radius: 10rpx;
  248. border-top-color: #fb5353;
  249. position: absolute;
  250. bottom: -46rpx;
  251. left: 50%;
  252. transform: translateX(-50%);
  253. transition: all 0.3s;
  254. }
  255. .prompt-layer-1 {
  256. font-size: 12px;
  257. width: 150px;
  258. text-align: center;
  259. display: flex;
  260. flex-direction: column;
  261. align-items: center;
  262. justify-content: center;
  263. top: -400rpx;
  264. }
  265. .prompt-layer-1 .p {
  266. color: #000000;
  267. }
  268. .prompt-layer-1 .span {
  269. color: rgba(0, 0, 0, 0.6);
  270. }
  271. .prompt-loader .em {
  272. }
  273. /* 语音音阶------------- */
  274. .prompt-loader {
  275. width: 96px;
  276. height: 20px;
  277. display: flex;
  278. align-items: center;
  279. justify-content: space-between;
  280. margin-bottom: 6px;
  281. }
  282. .prompt-loader .em {
  283. display: block;
  284. background: #333333;
  285. width: 1px;
  286. height: 10%;
  287. margin-right: 2.5px;
  288. float: left;
  289. }
  290. .prompt-loader .em:last-child {
  291. margin-right: 0px;
  292. }
  293. .prompt-loader .em:nth-child(1) {
  294. animation: load 2.5s 1.4s infinite linear;
  295. }
  296. .prompt-loader .em:nth-child(2) {
  297. animation: load 2.5s 1.2s infinite linear;
  298. }
  299. .prompt-loader .em:nth-child(3) {
  300. animation: load 2.5s 1s infinite linear;
  301. }
  302. .prompt-loader .em:nth-child(4) {
  303. animation: load 2.5s 0.8s infinite linear;
  304. }
  305. .prompt-loader .em:nth-child(5) {
  306. animation: load 2.5s 0.6s infinite linear;
  307. }
  308. .prompt-loader .em:nth-child(6) {
  309. animation: load 2.5s 0.4s infinite linear;
  310. }
  311. .prompt-loader .em:nth-child(7) {
  312. animation: load 2.5s 0.2s infinite linear;
  313. }
  314. .prompt-loader .em:nth-child(8) {
  315. animation: load 2.5s 0s infinite linear;
  316. }
  317. .prompt-loader .em:nth-child(9) {
  318. animation: load 2.5s 0.2s infinite linear;
  319. }
  320. .prompt-loader .em:nth-child(10) {
  321. animation: load 2.5s 0.4s infinite linear;
  322. }
  323. .prompt-loader .em:nth-child(11) {
  324. animation: load 2.5s 0.6s infinite linear;
  325. }
  326. .prompt-loader .em:nth-child(12) {
  327. animation: load 2.5s 0.8s infinite linear;
  328. }
  329. .prompt-loader .em:nth-child(13) {
  330. animation: load 2.5s 1s infinite linear;
  331. }
  332. .prompt-loader .em:nth-child(14) {
  333. animation: load 2.5s 1.2s infinite linear;
  334. }
  335. .prompt-loader .em:nth-child(15) {
  336. animation: load 2.5s 1.4s infinite linear;
  337. }
  338. @keyframes load {
  339. 0% {
  340. height: 10%;
  341. }
  342. 50% {
  343. height: 100%;
  344. }
  345. 100% {
  346. height: 10%;
  347. }
  348. }
  349. /* 语音音阶-------------------- */
  350. .prompt-layer-2 {
  351. top: -40px;
  352. }
  353. .prompt-layer-2 .text {
  354. color: rgba(0, 0, 0, 1);
  355. font-size: 12px;
  356. }
  357. /* 语音录制结束---------------------------------------------------------------- */
  358. </style>