recording.vue 7.2 KB

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