recording2.vue 8.8 KB

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