create-inner-audio-context.uvue 9.5 KB
Newer Older
1
<template>
M
mahaifeng 已提交
2 3 4 5 6 7
	<!-- #ifdef APP -->
	<scroll-view style="flex: 1;">
	<!-- #endif -->
		<view class="uni-padding-wrap">
			<page-head title="audio"></page-head>
			<view class="uni-common-mt">
8 9
				<slider ref="slider" :value="position" :min="0" :max="duration" @changing="onchanging"
					@change="onchange"></slider>
M
mahaifeng 已提交
10 11 12 13 14 15 16 17 18 19 20 21
			</view>
			<view class="uni-title">
				<text class="uni-title-text">属性示例</text>
			</View>
			<text class="uni-text-box uni-common-mt">当前音频播放位置(保留小数点后 6 位):{{currentTime}} s</text>
			<text class="uni-text-box">音频的长度(单位:s):{{duration}} s</text>
			<text class="uni-text-box">当前是否停止状态:{{isPaused}}</text>
			<text class="uni-text-box">音频缓冲的时间点:{{buffered}}</text>
			<text class="uni-text-box">当前音量:{{volume}}</text>
			<!-- 设置音量无效 -->
			<button plain :disabled="volume == 1" @click="increaseVolume">增加音量</button>
			<button plain :disabled="volume == 0" @click="decreaseVolume">减少音量</button>
22

M
mahaifeng 已提交
23 24 25 26 27 28
			<text class="uni-subtitle-text uni-title">开始播放的位置(单位:s)</text>
			<input :value="startTime" type="number" placeholder="开始播放的位置(单位:s)" class="uni-input"
				@input="startTimeInput"></input>
			<boolean-data :defaultValue="false" title="是否自动开始播放" @change="setAutoplay"></boolean-data>
			<boolean-data :defaultValue="false" title="是否循环播放" @change="setLoop"></boolean-data>
			<text class="uni-subtitle-text uni-title"
29
				style="padding-left: 10px;padding-top: 10px;padding-right: 10px;">播放倍率(Web不支持)</text>
M
mahaifeng 已提交
30 31 32 33 34 35 36 37 38 39
			<radio-group class="uni-flex uni-row radio-group" @change="playbackRateChange"
				style="flex-wrap: wrap;padding: 10px;">
				<radio value="0.5" style="margin-right: 3px">0.5
				</radio>
				<radio value="0.8" style="margin-right: 3px">0.8</radio>
				<radio value="1.0" style="margin-right: 3px" :checked="playbackRateChecked">1.0</radio>
				<radio value="1.25" style="margin-right: 3px">1.25</radio>
				<radio value="1.5" style="margin-right: 3px">1.5</radio>
				<radio value="2.0">2.0</radio>
			</radio-group>
40

M
mahaifeng 已提交
41 42 43
			<view class="uni-title">
				<text class="uni-title-text">方法示例</text>
			</View>
44 45 46 47 48 49 50 51
			<button :disabled="isPlaying" @click="play" class="uni-btn">播放</button>
			<button :disabled="!isPlaying" @click="pause" class="uni-btn">暂停</button>
			<button :disabled="!isPlaying" @click="stop" class="uni-btn">停止</button>
			<button @click="onchangeValue(20)" class="uni-btn">跳转到指定位置20</button>
			<button @click="onTimeUpdate" class="uni-btn">onTimeUpdate</button>
			<button @click="offTimeUpdate" class="uni-btn">offTimeUpdate</button>
			<button @click="onWaiting" class="uni-btn">onWaiting</button>
			<button @click="offWaiting" class="uni-btn">offWaiting</button>
M
mahaifeng 已提交
52 53 54


			<text style="color: red;font-size: 15px;margin-top: 10px;">tip:销毁后请重新进入此界面再播放</text>
55
			<button @click="destory" class="uni-btn">销毁</button>
M
mahaifeng 已提交
56 57 58 59 60

			<view class="uni-title">
				<text class="uni-title-text">格式/路径示例</text>
			</View>
			<navigator url="/pages/API/create-inner-audio-context/inner-audio-format" class="uni-btn">
61
				<button @click="pause">音频格式示例</button>
M
mahaifeng 已提交
62
			</navigator>
M
mahaifeng 已提交
63
			<navigator url="/pages/API/create-inner-audio-context/inner-audio-path" class="uni-btn">
64
				<button @click="pause">音频路径示例</button>
M
mahaifeng 已提交
65
			</navigator>
M
mahaifeng 已提交
66
			<navigator url="/pages/API/create-inner-audio-context/inner-audio-mult" class="uni-btn">
67
				<button @click="pause">多音频同时播放</button>
68
			</navigator>
M
mahaifeng 已提交
69 70 71 72
		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
73 74
</template>
<script lang="uts">
M
mahaifeng 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	const audioUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
	export default {
		data() {
			return {
				title: "innerAudioContext",
				currentTime: 0,
				duration: 100,
				startTime: 0,
				buffered: 0,
				volume: 0.5,
				isCanplay: false,
				isPlaying: false,
				isPaused: true,
				isPlayEnd: false,
				_isChanging: false,
				_audioContext: null as InnerAudioContext | null,
				// 自动化测试
				onSeekingTest: false,
				onSeekedTest: false,
				onWaitingTest: false,
				playbackRateChecked: true,
				onTimeUpdateCb: (res : any) => { },
				onWaitingCb: (res : any) => { }
			}
		},
		computed: {
			position() {
				return this.isPlayEnd ? 0 : this.currentTime;
			},
		},
		onReady() {
			this._audioContext = uni.createInnerAudioContext();
			this._audioContext!.src = audioUrl;
			this.volume = this._audioContext!.volume;
			this.onCanplay()
			this._audioContext!.onPlay(() => {
				this.isPaused = false;
				this.isPlaying = true;
				console.log('开始播放', this.isPaused);
			});

			this.onTimeUpdateCb = (res : any) => {
				if (this._isChanging) { return; }
				this.currentTime = this._audioContext!.currentTime;
				console.log('onTimeUpdateCb', this.currentTime)
				if (this.currentTime > this.buffered) {
					console.log('缓冲不足');
				}
			};

			this.onWaitingCb = (res : any) => {
				console.log('音频加载中事件');
				this.onWaitingTest = true
			}

			this.onTimeUpdate()
			// this.onWaiting()
			this.onError()
			this.onEnded()
		},
		onUnload() {
M
mahaifeng 已提交
136
			if (this._audioContext != null) {
137 138 139
				if (this.isPlaying) {
					this.stop();
				}
M
mahaifeng 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
				this._audioContext!.destroy()
			}
		},
		methods: {
			onCanplay() {
				this._audioContext!.onCanplay(() => {
					console.log('音频进入可以播放状态事件');
					this.isCanplay = true;
					// 当音频可以播放时,获取缓冲信息
					this.buffered = this._audioContext!.buffered;
					this.duration = this._audioContext!.duration
				});
			},
			onchanging() {
				this._isChanging = true;
			},
			onchange(e : UniSliderChangeEvent) {
				let pos = e.detail.value;
M
mahaifeng 已提交
158
				console.log('pos', pos);
M
mahaifeng 已提交
159 160 161 162 163 164 165 166 167 168 169
				this._audioContext!.seek(pos);
				this.onSeeking()
				this.onSeeked()
				this._isChanging = false;
			},
			onchangeValue(pos : number) {
				this._audioContext!.seek(pos);
				this.onSeeking()
				this.onSeeked()
				this._isChanging = false;
			},
M
mahaifeng 已提交
170
			startTimeInput(e : UniInputEvent) {
M
mahaifeng 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
				let startTimeValue = parseInt(e.detail.value)
				this._audioContext!.startTime = startTimeValue;
				this.onchangeValue(startTimeValue)
			},
			setAutoplay() {
				this._audioContext!.autoplay = !this._audioContext!.autoplay;
				console.log(this._audioContext!.autoplay, 'autoplay');
			},
			setLoop() {
				this._audioContext!.loop = !this._audioContext!.loop;
				console.log(this._audioContext!.loop, 'loop');
			},
			play() {
				if (!this.isCanplay) {
					uni.showToast({
M
mahaifeng 已提交
186 187
						title: '音频未进入可以播放状态,请稍后再试',
						icon: 'error'
M
mahaifeng 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
					});
					return;
				}
				this.isPlaying = true;
				this._audioContext!.play();
				this.isPlayEnd = false;
				if (this._audioContext!.startTime > 0) {
					this.onchangeValue(this._audioContext!.startTime)
				}
			},
			onSeeking() {
				this._audioContext!.onSeeking(() => {
					console.log('音频进行 seek 操作事件');
					this.onSeekingTest = true
				});
			},
			onSeeked() {
				this._audioContext!.onSeeked(() => {
					console.log('音频完成 seek 操作事件');
					this.onSeekedTest = true
				});
			},
			onWaiting() {
				this._audioContext!.onWaiting(this.onWaitingCb);
			},
			offWaiting() {
				this._audioContext!.offWaiting(this.onWaitingCb);
			},
			onTimeUpdate() {
				this._audioContext!.onTimeUpdate(this.onTimeUpdateCb);
			},
			offTimeUpdate() {
				this._audioContext!.offTimeUpdate(this.onTimeUpdateCb);
			},
			increaseVolume() {
				this.volume = Math.min(this.volume + 0.1, 1);
				this.volume = parseFloat(this.volume.toFixed(1));
				this._audioContext!.volume = this.volume
				console.log('增加音量', this.volume);
			},
			decreaseVolume() {
				this.volume = Math.max(this.volume - 0.1, 0);
				this.volume = parseFloat(this.volume.toFixed(1));
				console.log('减少音量', this.volume);
				this._audioContext!.volume = this.volume
			},
			onEnded() {
				this._audioContext!.onEnded(() => {
					console.log('播放结束');
					this.currentTime = 0;
					this.startTime = 0
					this.isPlaying = false;
					this.isPaused = true;
					this.isPlayEnd = true;
242
					(this.$refs["slider"] as UniSliderElement).value = 0
M
mahaifeng 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
				});
			},
			onError() {
				this._audioContext!.onError((err) => {
					console.log('err', err);
					this.isPlaying = false;
					this.isPaused = true;
				});
			},
			pause() {
				this._audioContext!.pause();
				this._audioContext!.onPause(() => {
					console.log('音频暂停事件');
					this.isPaused = true;
				});
				this.isPlaying = false;
			},
			stop() {
				console.log('stop');
				this._audioContext!.stop();
				this._audioContext!.onStop(() => {
					// 第一次点停止时,不触发
					this.isPaused = true;
					console.log('音频停止事件');
				});
				this.isPlaying = false;
				console.log('stop', this.isPaused);
			},
			destory() {
				if (this._audioContext != null) {
					this.isPlaying = false;
					this._audioContext!.destroy()
				}
			},
			playbackRateChange(e : UniRadioGroupChangeEvent) {
M
mahaifeng 已提交
278
				// if (this._audioContext != null && this.isPlaying) {
M
mahaifeng 已提交
279
					console.log(parseFloat(e.detail.value))
M
mahaifeng 已提交
280
					this._audioContext!.playbackRate = parseFloat(e.detail.value)
M
mahaifeng 已提交
281
				// }
M
mahaifeng 已提交
282 283 284
			},
		}
	}
285 286
</script>
<style>
M
mahaifeng 已提交
287 288 289 290 291
	.play-time-area {
		display: flex;
		flex-direction: row;
		margin-top: 20px;
	}
292

M
mahaifeng 已提交
293 294 295
	.duration {
		margin-left: auto;
	}
296

M
mahaifeng 已提交
297 298 299 300 301 302
	.play-button-area {
		display: flex;
		flex-direction: row;
		justify-content: center;
		margin: 50px 0;
	}
303

M
mahaifeng 已提交
304 305 306 307
	.icon-play {
		width: 60px;
		height: 60px;
	}
308
</style>