|
| 1 | +<template> |
| 2 | + <view class="uni-padding-wrap"> |
| 3 | + <page-head title="audio"></page-head> |
| 4 | + <view class="uni-common-mt"> |
| 5 | + <slider :value="position" :min="0" :max="duration" @changing="onchanging" @change="onchange"></slider> |
| 6 | + </view> |
| 7 | + <!-- <view class="uni-common-mt play-time-area"> |
| 8 | + <text class="current-time">{{currentTime}}</text> |
| 9 | + <text class="duration">{{duration}}</text> |
| 10 | + </view> --> |
| 11 | + <view class="play-button-area"> |
| 12 | + <image class="icon-play" :src="playImage" @click="play"></image> |
| 13 | + </view> |
| 14 | + </view> |
| 15 | +</template> |
| 16 | +<script> |
| 17 | + const audioUrl = 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/audio/music.mp3' |
| 18 | + export default { |
| 19 | + data() { |
| 20 | + return { |
| 21 | + title: "innerAudioContext", |
| 22 | + isPlaying: false, |
| 23 | + isPlayEnd: false, |
| 24 | + currentTime: 0, |
| 25 | + duration: 100 |
| 26 | + } |
| 27 | + }, |
| 28 | + computed: { |
| 29 | + position() { |
| 30 | + return this.isPlayEnd ? 0 : this.currentTime; |
| 31 | + }, |
| 32 | + playImage() { |
| 33 | + return this.isPlaying ? "/static/pause.png" : "/static/play.png" |
| 34 | + } |
| 35 | + }, |
| 36 | + onLoad() { |
| 37 | + this._isChanging = false; |
| 38 | + this._audioContext = null; |
| 39 | + this.createAudio(); |
| 40 | + }, |
| 41 | + onUnload() { |
| 42 | + if (this._audioContext != null && this.isPlaying) { |
| 43 | + this.stop(); |
| 44 | + } |
| 45 | + }, |
| 46 | + methods: { |
| 47 | + createAudio() { |
| 48 | + var innerAudioContext = this._audioContext = uni.createInnerAudioContext(); |
| 49 | + innerAudioContext.autoplay = false; |
| 50 | + innerAudioContext.src = audioUrl; |
| 51 | + innerAudioContext.onPlay(() => { |
| 52 | + console.log('开始播放'); |
| 53 | + }); |
| 54 | + innerAudioContext.onTimeUpdate((e) => { |
| 55 | + if (this._isChanging === true) { |
| 56 | + return; |
| 57 | + } |
| 58 | + this.currentTime = innerAudioContext.currentTime || 0; |
| 59 | + this.duration = innerAudioContext.duration || 0; |
| 60 | + }); |
| 61 | + innerAudioContext.onEnded(() => { |
| 62 | + this.currentTime = 0; |
| 63 | + this.isPlaying = false; |
| 64 | + this.isPlayEnd = true; |
| 65 | + }); |
| 66 | + innerAudioContext.onError((res) => { |
| 67 | + this.isPlaying = false; |
| 68 | + console.log(res.errMsg); |
| 69 | + console.log(res.errCode); |
| 70 | + }); |
| 71 | + return innerAudioContext; |
| 72 | + }, |
| 73 | + onchanging() { |
| 74 | + this._isChanging = true; |
| 75 | + }, |
| 76 | + onchange(e) { |
| 77 | + console.log(e.detail.value); |
| 78 | + console.log(typeof e.detail.value); |
| 79 | + this._audioContext.seek(e.detail.value); |
| 80 | + this._isChanging = false; |
| 81 | + }, |
| 82 | + play() { |
| 83 | + if (this.isPlaying) { |
| 84 | + this.pause(); |
| 85 | + return; |
| 86 | + } |
| 87 | + this.isPlaying = true; |
| 88 | + this._audioContext.play(); |
| 89 | + this.isPlayEnd = false; |
| 90 | + }, |
| 91 | + pause() { |
| 92 | + this._audioContext.pause(); |
| 93 | + this.isPlaying = false; |
| 94 | + }, |
| 95 | + stop() { |
| 96 | + this._audioContext.stop(); |
| 97 | + this.isPlaying = false; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +</script> |
| 102 | +<style> |
| 103 | + .play-time-area { |
| 104 | + display: flex; |
| 105 | + flex-direction: row; |
| 106 | + margin-top: 20px; |
| 107 | + } |
| 108 | +
|
| 109 | + .duration { |
| 110 | + margin-left: auto; |
| 111 | + } |
| 112 | +
|
| 113 | + .play-button-area { |
| 114 | + display: flex; |
| 115 | + flex-direction: row; |
| 116 | + justify-content: center; |
| 117 | + margin-top: 50px; |
| 118 | + } |
| 119 | +
|
| 120 | + .icon-play { |
| 121 | + width: 60px; |
| 122 | + height: 60px; |
| 123 | + } |
| 124 | +</style> |
0 commit comments