recording3.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view class="record-layer">
  3. <view class="record-box">
  4. <view class="record-btn-layer" v-if="tempFilePath == ''">
  5. <button class="record-btn" :class="longPress == '1' ? 'record-btn-1' : 'record-btn-2'" @longpress="longpressBtn()" @touchend="touchendBtn()">
  6. <image src="../../static/image/logo.png" />
  7. <text>{{ longPress == '1' ? '按住说话' : '说话中...' }}</text>
  8. </button>
  9. </view>
  10. <view class="record-btn-layer" v-else>
  11. <button class="record-btn" @longpress="delShow = true" @click="playBtn()" :class="playStatus == '1' ? 'record-btn-2' : 'record-btn-1'">
  12. <image src="../../static/image/logo.png" />
  13. <text>{{ playStatus == '1' ? count + 's' : '点击播放' }}</text>
  14. </button>
  15. </view>
  16. <!-- 语音音阶动画 -->
  17. <view class="prompt-layer prompt-layer-1" v-if="longPress == '2'">
  18. <view class="prompt-loader">
  19. <view class="em" v-for="(item, index) in 15" :key="index"></view>
  20. </view>
  21. <text class="p">{{ '剩余:' + count + 's' }}</text>
  22. <text class="span">松手结束录音</text>
  23. </view>
  24. <!-- 删除 -->
  25. <view class="prompt-layer prompt-layer-2" v-if="delShow" @click.stop="delBtn()">
  26. <text>删除</text>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. const recorderManager = uni.getRecorderManager()
  33. const innerAudioContext = uni.createInnerAudioContext()
  34. var init // 录制时长计时器
  35. var timer // 播放 录制倒计时
  36. export default {
  37. data() {
  38. return {
  39. count: null, // 录制倒计时
  40. longPress: '1', // 1显示 按住说话 2显示 说话中
  41. delShow: false, // 删除提示框显示隐藏
  42. time: 0, //录音时长
  43. duration: 60000, //录音最大值ms 60000/1分钟
  44. tempFilePath: '', //音频路径
  45. playStatus: 0 //录音播放状态 0:未播放 1:正在播放
  46. }
  47. },
  48. methods: {
  49. // 倒计时
  50. countdown(val) {
  51. this.count = Number(val)
  52. timer = setInterval(() => {
  53. if (this.count > 0) {
  54. this.count--
  55. } else {
  56. this.longPress = '1'
  57. clearInterval(timer)
  58. }
  59. }, 1000)
  60. },
  61. // 长按录音事件
  62. longpressBtn() {
  63. this.longPress = '2'
  64. this.countdown(60) // 倒计时
  65. clearInterval(init) // 清除定时器
  66. recorderManager.onStop((res) => {
  67. this.tempFilePath = res.tempFilePath
  68. this.recordingTimer(this.time)
  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.recordingTimer()
  79. recorderManager.start(options)
  80. // 监听音频开始事件
  81. recorderManager.onStart((res) => {
  82. console.log(res)
  83. })
  84. },
  85. // 长按松开录音事件
  86. touchendBtn() {
  87. this.longPress = '1'
  88. recorderManager.onStop((res) => {
  89. this.tempFilePath = res.tempFilePath
  90. })
  91. this.recordingTimer(this.time)
  92. recorderManager.stop()
  93. },
  94. recordingTimer(time) {
  95. if (time == undefined) {
  96. // 将计时器赋值给init
  97. init = setInterval(() => {
  98. this.time++
  99. }, 1000)
  100. } else {
  101. clearInterval(init)
  102. }
  103. },
  104. // 删除录音
  105. delBtn() {
  106. this.delShow = false
  107. this.time = 0
  108. this.tempFilePath = ''
  109. this.playStatus = 0
  110. innerAudioContext.stop()
  111. },
  112. // 播放
  113. playBtn() {
  114. innerAudioContext.src = this.tempFilePath
  115. //在ios下静音时播放没有声音,默认为true,改为false就好了。
  116. // innerAudioContext.obeyMuteSwitch = false
  117. //点击播放
  118. if (this.playStatus == 0) {
  119. this.playStatus = 1
  120. innerAudioContext.play()
  121. this.countdown(this.time) // 倒计时
  122. } else {
  123. this.playStatus = 0
  124. innerAudioContext.pause()
  125. }
  126. // //播放结束
  127. innerAudioContext.onEnded(() => {
  128. this.playStatus = 0
  129. innerAudioContext.stop()
  130. })
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped>
  136. /* 语音录制开始--------------------------------------------------------------------- */
  137. .record-layer {
  138. margin: auto;
  139. width: 50%;
  140. padding: 300px 0;
  141. box-sizing: border-box;
  142. }
  143. .record-box {
  144. width: 100%;
  145. position: relative;
  146. }
  147. .record-btn-layer {
  148. width: 100%;
  149. }
  150. .record-btn-layer button::after {
  151. border: none;
  152. }
  153. .record-btn-layer button {
  154. font-size: 14px;
  155. line-height: 38px;
  156. width: 100%;
  157. height: 38px;
  158. border-radius: 8px;
  159. text-align: center;
  160. background: #ffd300;
  161. }
  162. .record-btn-layer button image {
  163. width: 16px;
  164. height: 16px;
  165. margin-right: 4px;
  166. vertical-align: middle;
  167. }
  168. .record-btn-layer .record-btn-2 {
  169. background: rgba(255, 211, 0, 0.2);
  170. }
  171. /* 提示小弹窗 */
  172. .prompt-layer {
  173. border-radius: 8px;
  174. background: #ffd300;
  175. padding: 8px 16px;
  176. box-sizing: border-box;
  177. position: absolute;
  178. left: 50%;
  179. transform: translateX(-50%);
  180. }
  181. .prompt-layer::after {
  182. content: '';
  183. display: block;
  184. border: 6px solid rgba(0, 0, 0, 0);
  185. border-top-color: rgba(255, 211, 0, 1);
  186. position: absolute;
  187. bottom: -10px;
  188. left: 50%;
  189. transform: translateX(-50%);
  190. }
  191. .prompt-layer-1 {
  192. font-size: 12px;
  193. width: 128px;
  194. text-align: center;
  195. display: flex;
  196. flex-direction: column;
  197. align-items: center;
  198. justify-content: center;
  199. top: -80px;
  200. }
  201. .prompt-layer-1 .p {
  202. color: #000000;
  203. }
  204. .prompt-layer-1 .span {
  205. color: rgba(0, 0, 0, 0.6);
  206. }
  207. .prompt-loader .em {
  208. }
  209. /* 语音音阶------------- */
  210. .prompt-loader {
  211. width: 96px;
  212. height: 20px;
  213. display: flex;
  214. align-items: center;
  215. justify-content: space-between;
  216. margin-bottom: 6px;
  217. }
  218. .prompt-loader .em {
  219. display: block;
  220. background: #333333;
  221. width: 1px;
  222. height: 10%;
  223. margin-right: 2.5px;
  224. float: left;
  225. }
  226. .prompt-loader .em:last-child {
  227. margin-right: 0px;
  228. }
  229. .prompt-loader .em:nth-child(1) {
  230. animation: load 2.5s 1.4s infinite linear;
  231. }
  232. .prompt-loader .em:nth-child(2) {
  233. animation: load 2.5s 1.2s infinite linear;
  234. }
  235. .prompt-loader .em:nth-child(3) {
  236. animation: load 2.5s 1s infinite linear;
  237. }
  238. .prompt-loader .em:nth-child(4) {
  239. animation: load 2.5s 0.8s infinite linear;
  240. }
  241. .prompt-loader .em:nth-child(5) {
  242. animation: load 2.5s 0.6s infinite linear;
  243. }
  244. .prompt-loader .em:nth-child(6) {
  245. animation: load 2.5s 0.4s infinite linear;
  246. }
  247. .prompt-loader .em:nth-child(7) {
  248. animation: load 2.5s 0.2s infinite linear;
  249. }
  250. .prompt-loader .em:nth-child(8) {
  251. animation: load 2.5s 0s infinite linear;
  252. }
  253. .prompt-loader .em:nth-child(9) {
  254. animation: load 2.5s 0.2s infinite linear;
  255. }
  256. .prompt-loader .em:nth-child(10) {
  257. animation: load 2.5s 0.4s infinite linear;
  258. }
  259. .prompt-loader .em:nth-child(11) {
  260. animation: load 2.5s 0.6s infinite linear;
  261. }
  262. .prompt-loader .em:nth-child(12) {
  263. animation: load 2.5s 0.8s infinite linear;
  264. }
  265. .prompt-loader .em:nth-child(13) {
  266. animation: load 2.5s 1s infinite linear;
  267. }
  268. .prompt-loader .em:nth-child(14) {
  269. animation: load 2.5s 1.2s infinite linear;
  270. }
  271. .prompt-loader .em:nth-child(15) {
  272. animation: load 2.5s 1.4s infinite linear;
  273. }
  274. @keyframes load {
  275. 0% {
  276. height: 10%;
  277. }
  278. 50% {
  279. height: 100%;
  280. }
  281. 100% {
  282. height: 10%;
  283. }
  284. }
  285. /* 语音音阶-------------------- */
  286. .prompt-layer-2 {
  287. top: -40px;
  288. }
  289. .prompt-layer-2 .text {
  290. color: rgba(0, 0, 0, 1);
  291. font-size: 12px;
  292. }
  293. /* 语音录制结束---------------------------------------------------------------- */
  294. </style>