my-audio.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <template>
  2. <view class="audio_container">
  3. <view class="title-box">
  4. {{ title }}
  5. </view>
  6. <view>
  7. <slider :backgroundColor='backgroundColor' :activeColor='activeColor' @change="handleSliderChange"
  8. :value="sliderIndex" :max="maxSliderIndex" block-color="#C30D23" block-size="16" />
  9. </view>
  10. <view style="padding: 0px 7.5px 0px 7.5px ; display: block; ">
  11. <view style="float: left; font-size: 10px;color:#848484;">
  12. {{ currentTimeText }}
  13. </view>
  14. <view style="float: right;font-size: 10px;color:#848484;">
  15. {{ totalTimeText }}
  16. </view>
  17. </view>
  18. <view style="margin-top: 35px;">
  19. <view class="control">
  20. <view class="uni-grid-icon">
  21. <image @tap="handleFastRewind" src="../static/images/get-back.svg"
  22. style="width: 24px;height: 24px;top:3px;">
  23. </image>
  24. </view>
  25. <view class="uni-grid-icon">
  26. <image @tap="handleChangeAudioState" v-show="!isPlaying" src="../static/images/play.svg"
  27. style="width: 24px;height: 24px;top:3px;">
  28. </image>
  29. <image @tap="handleChangeAudioState" v-show="isPlaying" src="../static/images/pause.svg"
  30. style="width: 24px;height: 24px;top:3px;">
  31. </image>
  32. </view>
  33. <view class="uni-grid-icon">
  34. <image @tap="handleFastForward" src="../static/images/fast-forward.svg"
  35. style="width: 24px;height: 24px;top:3px;">
  36. </image>
  37. </view>
  38. <!-- <view class="uni-grid-icon">
  39. <image @tap="handleLoopPlay" src="../static/images/Loop.svg"
  40. style="width: 24px;height: 24px; top:3px; ">
  41. </image>
  42. </view> -->
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. export default {
  49. name: 'my-audio',
  50. //audioPlay开始播放
  51. //audioPause停止播放
  52. //audioEnd音频自然播放结束事件
  53. //audioCanplay音频进入可以播放状态,但不保证后面可以流畅播放
  54. //change播放状态改变 返回值false停止播放 true开始播放
  55. //audioError 播放器错误
  56. emits: ['audioPlay', 'audioPause', 'audioEnd', 'audioCanplay', 'change', 'audioError'],
  57. props: {
  58. title: {
  59. type: String
  60. },
  61. //是否自动播放
  62. autoplay: {
  63. type: Boolean,
  64. default: false
  65. },
  66. //滑块左侧已选择部分的线条颜色
  67. activeColor: {
  68. type: String,
  69. default: '#C30D23'
  70. },
  71. //滑块右侧背景条的颜色
  72. backgroundColor: {
  73. type: String,
  74. default: '#E5E5E5'
  75. },
  76. //音频地址
  77. src: {
  78. type: [String, Array],
  79. default: ''
  80. },
  81. //是否倒计时
  82. isCountDown: {
  83. type: Boolean,
  84. default: false
  85. },
  86. //音乐封面
  87. audioCover: {
  88. type: String,
  89. default: ''
  90. },
  91. //是否显示收藏按钮
  92. isCollectBtn: {
  93. type: Boolean,
  94. default: false
  95. },
  96. //是否显示分享按钮
  97. isShareBtn: {
  98. type: Boolean,
  99. default: false
  100. },
  101. },
  102. data() {
  103. return {
  104. totalTimeText: '00:00', //视频总长度文字
  105. currentTimeText: '00:00:00', //视频已播放长度文字
  106. isPlaying: false, //播放状态
  107. sliderIndex: 0, //滑块当前值
  108. maxSliderIndex: 100, //滑块最大值
  109. IsReadyPlay: false, //是否已经准备好可以播放了
  110. isLoop: false, //是否循环播放
  111. speedValue: [0.5, 0.8, 1.0, 1.25, 1.5, 2.0],
  112. speedValueIndex: 2,
  113. playSpeed: '1.0', //播放倍速 可取值:0.5/0.8/1.0/1.25/1.5/2.0
  114. stringObject: (data) => {
  115. return typeof (data)
  116. },
  117. innerAudioContext: uni.createInnerAudioContext()
  118. }
  119. },
  120. async mounted() {
  121. this.innerAudioContext.src = typeof (this.src) == 'string' ? this.src : this.src[0];
  122. if (this.autoplay) {
  123. if (!this.src) return console.error('src cannot be empty,The target value is string or array')
  124. // #ifdef H5
  125. var ua = window.navigator.userAgent.toLowerCase();
  126. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  127. const jweixin = require('../utils/jweixin');
  128. jweixin.config({});
  129. jweixin.ready(() => {
  130. WeixinJSBridge.invoke('getNetworkType', {}, (e) => {
  131. this.innerAudioContext.play();
  132. })
  133. })
  134. }
  135. // #endif
  136. // #ifndef H5
  137. this.innerAudioContext.autoplay = true;
  138. // #endif
  139. }
  140. //音频播放事件
  141. this.innerAudioContext.onPlay(() => {
  142. this.isPlaying = true;
  143. this.$emit('audioPlay')
  144. this.$emit('change', {
  145. state: true
  146. });
  147. setTimeout(() => {
  148. this.maxSliderIndex = parseFloat(this.innerAudioContext.duration).toFixed(2);
  149. }, 100)
  150. });
  151. //音频暂停事件
  152. this.innerAudioContext.onPause(() => {
  153. this.$emit('audioPause');
  154. this.$emit('change', {
  155. state: false
  156. });
  157. });
  158. //音频自然播放结束事件
  159. this.innerAudioContext.onEnded(() => {
  160. this.isPlaying = !this.isPlaying;
  161. this.$emit('audioEnd');
  162. if (this.isLoop) {
  163. this.changePlayProgress(0);
  164. this.innerAudioContext.play();
  165. }
  166. });
  167. //音频进入可以播放状态,但不保证后面可以流畅播放
  168. this.innerAudioContext.onCanplay((event) => {
  169. this.IsReadyPlay = true;
  170. this.$emit('audioCanplay');
  171. let duration = this.innerAudioContext.duration;
  172. //console.log('总时长', duration)
  173. //将当前音频长度秒转换为00:00:00格式
  174. this.totalTimeText = this.getFormateTime(duration);
  175. this.maxSliderIndex = parseFloat(duration).toFixed(2);
  176. //console.log(this.getFormateTime(duration))
  177. //console.log('总时长1', this.totalTimeText)
  178. //防止视频无法正确获取时长
  179. setTimeout(() => {
  180. duration = this.innerAudioContext.duration;
  181. //将当前音频长度秒转换为00:00:00格式
  182. this.totalTimeText = this.getFormateTime(duration);
  183. this.maxSliderIndex = parseFloat(duration).toFixed(2);
  184. //console.log('总时长2', this.totalTimeText)
  185. }, 300)
  186. });
  187. //音频播放错误事件
  188. this.innerAudioContext.onTimeUpdate((res) => {
  189. this.sliderIndex = parseFloat(this.innerAudioContext.currentTime).toFixed(2);
  190. this.currentTimeText = this.getFormateTime(this.innerAudioContext.currentTime);
  191. });
  192. //音频播放错误事件
  193. this.innerAudioContext.onError((res) => {
  194. console.log(res.errMsg);
  195. console.log(res.errCode);
  196. this.$emit('change', {
  197. state: false
  198. });
  199. this.audioPause();
  200. this.$emit('audioError', res);
  201. });
  202. },
  203. methods: {
  204. //销毁innerAudioContext()实例
  205. audioDestroy() {
  206. if (this.innerAudioContext) {
  207. this.innerAudioContext.destroy();
  208. this.isPlaying = false;
  209. }
  210. },
  211. //点击变更播放状态
  212. handleChangeAudioState() {
  213. if (this.isPlaying && !this.innerAudioContext.paused) {
  214. this.audioPause();
  215. } else {
  216. this.audioPlay();
  217. }
  218. },
  219. //开始播放
  220. audioPlay() {
  221. this.innerAudioContext.play();
  222. this.isPlaying = true;
  223. },
  224. //暂停播放
  225. audioPause() {
  226. this.innerAudioContext.pause();
  227. this.isPlaying = false;
  228. },
  229. //变更滑块位置
  230. handleSliderChange(e) {
  231. this.changePlayProgress(e.detail ? e.detail.value : e)
  232. },
  233. //更改播放倍速
  234. handleChageSpeed() {
  235. //获取播放倍速列表长度
  236. let speedCount = this.speedValue.length;
  237. //如果当前是最大倍速,从-1开始
  238. if (this.speedValueIndex == (speedCount - 1)) {
  239. this.speedValueIndex = -1;
  240. }
  241. //最新倍速序号
  242. this.speedValueIndex += 1;
  243. //获取最新倍速文字
  244. this.playSpeed = this.speedValue[this.speedValueIndex].toFixed(1);
  245. //暂停播放
  246. this.audioPause();
  247. //变更播放倍速
  248. this.innerAudioContext.playbackRate(this.speedValue[this.speedValueIndex]);
  249. //开始播放
  250. this.audioPlay();
  251. },
  252. //快退15秒
  253. handleFastRewind() {
  254. if (this.IsReadyPlay) {
  255. let value = parseInt(this.sliderIndex) - 15;
  256. this.changePlayProgress(value >= 0 ? value : 0);
  257. }
  258. },
  259. //快进15秒
  260. handleFastForward() {
  261. if (this.IsReadyPlay) {
  262. let value = parseInt(this.sliderIndex) + 15;
  263. this.changePlayProgress(value <= this.innerAudioContext.duration ? value : this.innerAudioContext
  264. .duration);
  265. }
  266. },
  267. //开启循环播放
  268. handleLoopPlay() {
  269. this.isLoop = !this.isLoop;
  270. if (this.isLoop) {
  271. uni.showToast({
  272. title: '已开启循环播放',
  273. duration: 1000
  274. });
  275. } else {
  276. uni.showToast({
  277. title: '取消循环播放',
  278. duration: 1000
  279. });
  280. }
  281. },
  282. //更改播放进度
  283. changePlayProgress(value) {
  284. this.innerAudioContext.seek(value);
  285. this.sliderIndex = value;
  286. this.currentTimeText = this.getFormateTime(value);
  287. },
  288. //秒转换为00:00:00
  289. getFormateTime(time) {
  290. let ms = time * 1000; // 1485000毫秒
  291. let date = new Date(ms);
  292. // 注意这里是使用的getUTCHours()方法,转换成UTC(协调世界时)时间的小时
  293. let hour = date.getUTCHours();
  294. // let hour = date.getHours(); 如果直接使用getHours()方法,则得到的时分秒格式会多出来8个小时(在国内开发基本都是使用的是东八区时间),getHours()方法会把当前的时区给加上。
  295. let minute = date.getMinutes();
  296. let second = date.getSeconds();
  297. let formatTime =
  298. `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:${second.toString().padStart(2, '0')}`;
  299. return formatTime;
  300. },
  301. handleCollec() {
  302. this.$emit('audioCollec');
  303. },
  304. handleShare() {
  305. this.$emit('audioShare');
  306. },
  307. },
  308. onUnload() {
  309. this.audioDestroy()
  310. },
  311. onHide() {
  312. this.audioDestroy()
  313. },
  314. beforeDestroy() {
  315. this.audioDestroy()
  316. }
  317. }
  318. </script>
  319. <style lang="scss" scoped>
  320. .control {
  321. display: flex;
  322. justify-content: space-around;
  323. padding: 0 30px;
  324. box-sizing: border-box;
  325. }
  326. .audio_container {
  327. box-shadow: 0 0 5px #c3c3c3;
  328. padding: 15px 10px 15px 10px;
  329. .audio-title {
  330. font-size: 14px;
  331. }
  332. .uni-noticebar {
  333. padding: 0px;
  334. padding-right: 25px;
  335. margin-bottom: 0px;
  336. display: inline-block;
  337. }
  338. .audio-subTitle {
  339. width: 100%;
  340. text-align: left;
  341. font-size: 20px;
  342. color: blue;
  343. }
  344. .speed-text {
  345. position: absolute;
  346. top: 0px;
  347. left: 15px;
  348. right: 0;
  349. color: #475266;
  350. font-size: 8px;
  351. font-weight: 600;
  352. }
  353. .uni-grid-icon {
  354. text-align: center;
  355. }
  356. }
  357. </style>