recording.vue 7.9 KB

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