recording.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(res)
  80. // console.log(this.sendLock)
  81. clearInterval(this.recordingTimer)
  82. if (this.sendLock) {
  83. //上锁不发送
  84. } else {
  85. //解锁发送,发送网络请求
  86. if (res.duration < 1000)
  87. uni.showToast({
  88. title: '录音时间太短',
  89. icon: 'none',
  90. duration: 1000
  91. })
  92. else {
  93. uni.showLoading({
  94. title: '上传中'
  95. })
  96. uni.uploadFile({
  97. url: `https://chtech.ncjti.edu.cn/campusMaintenance/repair-api/repair/api/repairRecord/uploadFile`,
  98. filePath: res.tempFilePath,
  99. name: 'file',
  100. header: {
  101. token: uni.getStorageSync('repairsUserInfo').token,
  102. user_head: uni.getStorageSync('repairsUserInfo').userhead
  103. },
  104. success: (uploadFileRes) => {
  105. // console.log(JSON.parse(uploadFileRes.data))
  106. this.tempFilePath = JSON.parse(uploadFileRes.data).data.resultUrl
  107. this.$emit('getTempFilePath', this.tempFilePath, this.recordingTime)
  108. uni.hideLoading()
  109. },
  110. fail: () => {
  111. uni.showToast({
  112. title: '上传失败',
  113. icon: 'error'
  114. })
  115. }
  116. })
  117. }
  118. }
  119. })
  120. this.recorderManager.stop() //结束录音
  121. },
  122. // 取消录音
  123. handleTouchMove(e) {
  124. //touchmove时触发
  125. var moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY //移动距离
  126. if (Math.abs(moveLenght) > 70) {
  127. this.VoiceTitle = '松开手指,取消录音'
  128. this.VoiceText = '松开手指,取消录音'
  129. //触发了上滑取消录音,上锁
  130. this.sendLock = true
  131. } else {
  132. this.VoiceTitle = '松手结束录音'
  133. this.VoiceText = '松手结束录音'
  134. //上划距离不足,依然可以发送,不上锁
  135. this.sendLock = false
  136. }
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .container {
  143. width: 100%;
  144. height: 100%;
  145. .record-btn-layer {
  146. display: flex;
  147. justify-content: center;
  148. align-items: center;
  149. width: 100%;
  150. height: 100%;
  151. button {
  152. width: 50%;
  153. height: 50px;
  154. line-height: 50px;
  155. text-align: center;
  156. font-size: 14px;
  157. border-radius: 8px;
  158. transition: all 0.1s;
  159. }
  160. button::after {
  161. border: none;
  162. transition: all 0.1s;
  163. }
  164. .record-btn-1 {
  165. background-color: #6fb6b8;
  166. color: #fff;
  167. }
  168. .record-btn-2 {
  169. color: #0061ff;
  170. transition: all 0.3s;
  171. }
  172. .record-btn-3 {
  173. background-color: #fb5353;
  174. color: white;
  175. }
  176. }
  177. }
  178. /* 提示小弹窗 */
  179. .prompt-layer {
  180. border-radius: 15px;
  181. background: #95eb6c;
  182. padding: 8px 16px;
  183. box-sizing: border-box;
  184. position: absolute;
  185. left: 50%;
  186. height: 11vh;
  187. transform: translateX(-50%);
  188. transition: all 0.3s;
  189. }
  190. .prompt-layer::after {
  191. content: '';
  192. display: block;
  193. border: 12px solid rgba(0, 0, 0, 0);
  194. border-radius: 10rpx;
  195. border-top-color: #95eb6c;
  196. position: absolute;
  197. bottom: -46rpx;
  198. left: 50%;
  199. transform: translateX(-50%);
  200. transition: all 0.3s;
  201. }
  202. //取消动画
  203. .prompt-layer1 {
  204. border-radius: 15px;
  205. background: #fb5353;
  206. padding: 8px 16px;
  207. box-sizing: border-box;
  208. position: absolute;
  209. left: 50%;
  210. height: 11vh;
  211. transform: translateX(-50%);
  212. transition: all 0.3s;
  213. }
  214. .prompt-layer1::after {
  215. content: '';
  216. display: block;
  217. border: 12px solid rgba(0, 0, 0, 0);
  218. border-radius: 10rpx;
  219. border-top-color: #fb5353;
  220. position: absolute;
  221. bottom: -46rpx;
  222. left: 50%;
  223. transform: translateX(-50%);
  224. transition: all 0.3s;
  225. }
  226. .prompt-layer-1 {
  227. font-size: 12px;
  228. width: 150px;
  229. text-align: center;
  230. display: flex;
  231. flex-direction: column;
  232. align-items: center;
  233. justify-content: center;
  234. top: -400rpx;
  235. .p {
  236. color: #000000;
  237. }
  238. .span {
  239. color: rgba(0, 0, 0, 0.6);
  240. }
  241. }
  242. /* 语音音阶------------- */
  243. .prompt-loader {
  244. width: 96px;
  245. height: 20px;
  246. display: flex;
  247. align-items: center;
  248. justify-content: space-between;
  249. margin-bottom: 6px;
  250. }
  251. .prompt-loader .em {
  252. display: block;
  253. background: #333333;
  254. width: 1px;
  255. height: 10%;
  256. margin-right: 2.5px;
  257. float: left;
  258. }
  259. .prompt-loader .em:last-child {
  260. margin-right: 0px;
  261. }
  262. .prompt-loader .em:nth-child(1) {
  263. animation: load 2.5s 1.4s infinite linear;
  264. }
  265. .prompt-loader .em:nth-child(2) {
  266. animation: load 2.5s 1.2s infinite linear;
  267. }
  268. .prompt-loader .em:nth-child(3) {
  269. animation: load 2.5s 1s infinite linear;
  270. }
  271. .prompt-loader .em:nth-child(4) {
  272. animation: load 2.5s 0.8s infinite linear;
  273. }
  274. .prompt-loader .em:nth-child(5) {
  275. animation: load 2.5s 0.6s infinite linear;
  276. }
  277. .prompt-loader .em:nth-child(6) {
  278. animation: load 2.5s 0.4s infinite linear;
  279. }
  280. .prompt-loader .em:nth-child(7) {
  281. animation: load 2.5s 0.2s infinite linear;
  282. }
  283. .prompt-loader .em:nth-child(8) {
  284. animation: load 2.5s 0s infinite linear;
  285. }
  286. .prompt-loader .em:nth-child(9) {
  287. animation: load 2.5s 0.2s infinite linear;
  288. }
  289. .prompt-loader .em:nth-child(10) {
  290. animation: load 2.5s 0.4s infinite linear;
  291. }
  292. .prompt-loader .em:nth-child(11) {
  293. animation: load 2.5s 0.6s infinite linear;
  294. }
  295. .prompt-loader .em:nth-child(12) {
  296. animation: load 2.5s 0.8s infinite linear;
  297. }
  298. .prompt-loader .em:nth-child(13) {
  299. animation: load 2.5s 1s infinite linear;
  300. }
  301. .prompt-loader .em:nth-child(14) {
  302. animation: load 2.5s 1.2s infinite linear;
  303. }
  304. .prompt-loader .em:nth-child(15) {
  305. animation: load 2.5s 1.4s infinite linear;
  306. }
  307. @keyframes load {
  308. 0% {
  309. height: 10%;
  310. }
  311. 50% {
  312. height: 100%;
  313. }
  314. 100% {
  315. height: 10%;
  316. }
  317. }
  318. /* 语音音阶-------------------- */
  319. .prompt-layer-2 {
  320. top: -40px;
  321. .text {
  322. color: rgba(0, 0, 0, 1);
  323. font-size: 12px;
  324. }
  325. }
  326. </style>