order.vue 8.1 KB

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