create-inner-audio-context.uvue 9.4 KB
Newer Older
M
mahaifeng 已提交
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>
M
mahaifeng 已提交
22

M
mahaifeng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
			<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"
				style="padding-left: 10px;padding-top: 10px;padding-right: 10px;">播放倍率</text>
			<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>
M
mahaifeng 已提交
40

M
mahaifeng 已提交
41 42 43 44 45
			<view class="uni-title">
				<text class="uni-title-text">方法示例</text>
			</View>
			<button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button>
			<button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button>
46
			<button :disabled="!isPlaying" type="primary" @click="stop" class="uni-btn">停止</button>
M
mahaifeng 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
			<button type="primary" @click="onchangeValue(20)" class="uni-btn">跳转到指定位置20</button>
			<button type="primary" @click="onTimeUpdate" class="uni-btn">onTimeUpdate</button>
			<button type="primary" @click="offTimeUpdate" class="uni-btn">offTimeUpdate</button>
			<button type="primary" @click="onWaiting" class="uni-btn">onWaiting</button>
			<button type="primary" @click="offWaiting" class="uni-btn">offWaiting</button>


			<text style="color: red;font-size: 15px;margin-top: 10px;">tip:销毁后请重新进入此界面再播放</text>
			<button type="primary" @click="destory" class="uni-btn">销毁</button>

			<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">
				<button type="primary" @click="pause">音频格式示例</button>
			</navigator>
			<navigator url="/pages/API/create-inner-audio-context/inner-audio-path" class="uni-btn uni-common-mb">
				<button type="primary" @click="pause">音频路径示例</button>
			</navigator>
		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
M
mahaifeng 已提交
70 71
</template>
<script lang="uts">
M
mahaifeng 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	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,
M
mahaifeng 已提交
93 94
				onTimeUpdateCb: (_ : any) => { },
				onWaitingCb: (_ : any) => { }
M
mahaifeng 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
			}
		},
		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);
			});

M
mahaifeng 已提交
113
			this.onTimeUpdateCb = (_ : any) => {
M
mahaifeng 已提交
114 115 116 117 118 119 120 121
				if (this._isChanging) { return; }
				this.currentTime = this._audioContext!.currentTime;
				console.log('onTimeUpdateCb', this.currentTime)
				if (this.currentTime > this.buffered) {
					console.log('缓冲不足');
				}
			};

M
mahaifeng 已提交
122
			this.onWaitingCb = (_ : any) => {
M
mahaifeng 已提交
123 124 125 126 127 128 129 130 131 132 133
				console.log('音频加载中事件');
				this.onWaitingTest = true
			}

			this.onTimeUpdate()
			// this.onWaiting()
			this.onError()
			this.onEnded()
		},
		onUnload() {
			if (this._audioContext != null && this.isPlaying) {
134 135 136
				if (this.isPlaying) {
					this.stop();
				}
M
mahaifeng 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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
				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) {
				console.log(e, 'e');
				let pos = e.detail.value;
				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;
			},
			startTimeInput(e : InputEvent) {
				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({
						title: '音频未进入可以播放状态,请稍后再试'
					});
					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;
238
					(this.$refs["slider"] as UniSliderElement).value = 0
M
mahaifeng 已提交
239 240 241 242 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 278 279 280
				});
			},
			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) {
				if (this._audioContext != null && this.isPlaying) {
					console.log(parseFloat(e.detail.value))
					this._audioContext!.playbackRate = parseFloat(e.detail.value)
				}
			},
		}
	}
M
mahaifeng 已提交
281 282
</script>
<style>
M
mahaifeng 已提交
283 284 285 286 287
	.play-time-area {
		display: flex;
		flex-direction: row;
		margin-top: 20px;
	}
M
mahaifeng 已提交
288

M
mahaifeng 已提交
289 290 291
	.duration {
		margin-left: auto;
	}
M
mahaifeng 已提交
292

M
mahaifeng 已提交
293 294 295 296 297 298
	.play-button-area {
		display: flex;
		flex-direction: row;
		justify-content: center;
		margin: 50px 0;
	}
M
mahaifeng 已提交
299

M
mahaifeng 已提交
300 301 302 303
	.icon-play {
		width: 60px;
		height: 60px;
	}
M
mahaifeng 已提交
304
</style>