inner-audio-format.uvue 3.7 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<template>
	<page-head :title="title"></page-head>
	<!-- #ifdef APP -->
	<scroll-view style="flex: 1;">
	<!-- #endif -->
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-title">
				<text class="uni-title-text">支持的音频格式示例</text>
			</view>

			<view class="formats" v-for="(item,index) in supportFormats" :key="index">
				<text class="uni-subtitle-text">{{item.format}}</text>
				<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
					@click="play(item.src,index)"></image>
			</view>

			<view class="uni-title">
				<text class="uni-title-text">不支持的音频格式</text>
			</view>
			<view class="formats" v-for="(item,index) in notSupportFormats" :key="index">
				<text class="uni-subtitle-text">{{item.format}}</text>
				<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
					@click="play(item.src,index)"></image>
			</view>

		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
</template>

<script>
	type AudioFormat = {
		format : string
		src : string
	}
	export default {
		data() {
			return {
				title: 'audio-format',
				playIndex: 0,
				isPlaying: false,
				_audioContext: null as InnerAudioContext | null,
				supportFormats: [
					{
						format: 'mp3',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
					},
					{
						format: 'mp4',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4'
					},
					{
						format: 'm4a',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a'
					},
					{
						format: 'aac',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac'
					},
					{
						format: 'flac',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac'
					},

					{
						format: 'ogg',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg'
					},
					{
						format: 'wav',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav'
					},
				] as Array<AudioFormat>,
				notSupportFormats: [
					{
						format: 'wma',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma'
					},
					{
						format: 'aiff',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff'
					},
					{
						format: 'caf',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf'
					},
					{
						format: '错误格式',
						src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa'
					},
				] as Array<AudioFormat>
			}
		},
		onReady() {
			this._audioContext = uni.createInnerAudioContext();
M
mahaifeng 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
			this._audioContext!.onPlay(() => {
				console.log('开始播放');
			});
			this._audioContext!.onPause(()=>{
				console.log('播放暂停');
			})
			this._audioContext!.onEnded(() => {
				console.log('播放结束');
				this.isPlaying = false;
			});
			this._audioContext!.onError((err) => {
				this.isPlaying = false;
				console.log('err', err);
			});
M
mahaifeng 已提交
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 136 137 138 139 140 141 142 143 144 145 146 147 148
		},
		onUnload() {
			if (this._audioContext != null) {
				this.pause();
				this._audioContext!.destroy()
			}
		},
		methods: {
			pause() {
				this._audioContext!.pause();
				this.isPlaying = false;
			},
			play(audioUrl : string, index : number) {
				// console.log(index,audioUrl);
				if (this.isPlaying && this.playIndex == index) {
					this.pause();
					return;
				}
				this.playIndex = index
				this._audioContext!.src = audioUrl;
				this._audioContext!.play();
				this.isPlaying = true;
			},
		},
	}
</script>

<style>
	.formats {
		align-items: center;
	}

	.icon-play {
		width: 60px;
		height: 60px;
		margin: 10px;
	}
</style>