order.vue 8.0 KB

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