inner-audio.uvue 5.3 KB
Newer Older
Anne_LXM's avatar
Anne_LXM 已提交
1 2 3 4 5 6 7 8 9
<template>
	<view class="uni-padding-wrap">
		<page-head title="audio"></page-head>
		<view class="uni-common-mt">
			<slider :value="position" :min="0" :max="duration" @changing="onchanging" @change="onchange"></slider>
		</view>
		<!-- <view class="uni-common-mt play-time-area">
			<text class="current-time">{{currentTime}}</text>
			<text class="duration">{{duration}}</text>
Anne_LXM's avatar
Anne_LXM 已提交
10
		</view>
Anne_LXM's avatar
Anne_LXM 已提交
11 12
		<view class="play-button-area">
			<image class="icon-play" :src="playImage" @click="play"></image>
Anne_LXM's avatar
Anne_LXM 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
		</view> -->
    <button type="primary" @click="play" class="uni-btn">播放</button>
    <button type="primary" @click="pause" class="uni-btn">暂停</button>
    <button type="primary" @click="stop" class="uni-btn">停止</button>
    <button type="primary" @click="onchange('seek')" class="uni-btn">跳转到指定位置</button>
    <button type="primary" @click="setAutoplay" class="uni-btn">是否自动开始播放</button>
    <button type="primary" @click="setLoop" class="uni-btn">是否循环播放</button>
    <view class="uni-title">
      <text class="uni-title-text">格式/路径示例</text>
    </View>
    <navigator url="/pages/API/inner-audio/inner-audio-format" class="uni-btn">
      <button type="primary" @click="pause">音频格式示例</button>
    </navigator>
    <navigator url="/pages/API/inner-audio/inner-audio-path" class="uni-btn">
      <button type="primary" @click="pause">音频路径示例</button>
    </navigator>
Anne_LXM's avatar
Anne_LXM 已提交
29 30 31 32 33 34 35 36 37 38 39 40
	</view>
</template>
<script lang="uts">
	const audioUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
	export default {
		data() {
			return {
				title: "innerAudioContext",
				isPlaying: false,
				isPlayEnd: false,
				currentTime: 0,
				duration: 100,
Anne_LXM's avatar
Anne_LXM 已提交
41 42
        pos:10,
        paused:false,
Anne_LXM's avatar
Anne_LXM 已提交
43 44 45 46 47 48 49 50 51 52 53 54
        _isChanging:false,
        _audioContext: null as InnerAudioContext | null
			}
		},
		computed: {
			position() {
				return this.isPlayEnd ? 0 : this.currentTime;
			},
			playImage() {
				return this.isPlaying ? "/static/pause.png" : "/static/play.png"
			}
		},
Anne_LXM's avatar
Anne_LXM 已提交
55 56 57 58 59
    onReady() {
      this._audioContext = uni.createInnerAudioContext();
      this._audioContext!.src = audioUrl;
      this.onCanplay()
    },
Anne_LXM's avatar
Anne_LXM 已提交
60 61 62
		onUnload() {
			if (this._audioContext != null && this.isPlaying) {
				this.stop();
Anne_LXM's avatar
Anne_LXM 已提交
63
        this._audioContext!.destroy()
Anne_LXM's avatar
Anne_LXM 已提交
64 65 66
			}
		},
		methods: {
Anne_LXM's avatar
Anne_LXM 已提交
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 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
      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');
      },
      onchanging() {
      	this._isChanging = true;
      },
      onSeeking(){
        this._audioContext!.onSeeking(() => {
          console.log('音频进行 seek 操作事件');
        });
      },
      onSeeked(){
        this._audioContext!.onSeeked(() => {
          console.log('音频完成 seek 操作事件');
        });
      },
      onchange(e) {
        let pos = e == 'seek' ? 20 : e.detail.value
      	this._audioContext!.seek(pos);
        this.onSeeking()
        this.onSeeked()
      	this._isChanging = false;
      },
      onCanplay(){
        this._audioContext!.onCanplay(() => {
          console.log('音频进入可以播放状态事件');
        });
      },
      onTimeUpdate(){
        this._audioContext!.onTimeUpdate((e) => {
          // console.log('onTimeUpdate:音频播放进度更新事件',e);
        	if (this._isChanging === true) {
        		return;
        	}
        	this.currentTime = this._audioContext!.currentTime || 0;
        	this.duration = this._audioContext!.duration || 0;
        });
      },
      onPlay(){
        this._audioContext!.onPlay(() => {
        	console.log('开始播放');
        });
      },
      onEnded(){
        this._audioContext!.onEnded(() => {
          console.log('播放结束');
        	this.currentTime = 0;
        	this.isPlaying = false;
        	this.isPlayEnd = true;
        });
      },
      onError(){
        this._audioContext!.onError((err) => {
          console.log('err',err);
        	this.isPlaying = false;
        });
      },
      onWaiting(){
        this._audioContext!.onWaiting(() => {
          console.log('音频加载中事件');
        });
      },
Anne_LXM's avatar
Anne_LXM 已提交
134
			play() {
Anne_LXM's avatar
Anne_LXM 已提交
135 136 137 138
				// if (this.isPlaying) {
				// 	this.pause();
				// 	return;
				// }
Anne_LXM's avatar
Anne_LXM 已提交
139 140
				this.isPlaying = true;
				this._audioContext!.play();
Anne_LXM's avatar
Anne_LXM 已提交
141 142 143 144 145 146
        this.isPlayEnd = false;
        this.onPlay()
        this.onWaiting()
        this.onTimeUpdate()
        this.onError()
        this.onEnded()
Anne_LXM's avatar
Anne_LXM 已提交
147
			},
Anne_LXM's avatar
Anne_LXM 已提交
148 149 150 151 152
      onPause(){
        this._audioContext!.onPause(() => {
          console.log('音频暂停事件');
        });
      },
Anne_LXM's avatar
Anne_LXM 已提交
153 154
			pause() {
				this._audioContext!.pause();
Anne_LXM's avatar
Anne_LXM 已提交
155
        this.onPause()
Anne_LXM's avatar
Anne_LXM 已提交
156 157
				this.isPlaying = false;
			},
Anne_LXM's avatar
Anne_LXM 已提交
158 159 160 161 162
      onStop(){
        this._audioContext!.onStop(() => {
          console.log('音频停止事件');
        });
      },
Anne_LXM's avatar
Anne_LXM 已提交
163 164
			stop() {
				this._audioContext!.stop();
Anne_LXM's avatar
Anne_LXM 已提交
165
        this.onStop()
Anne_LXM's avatar
Anne_LXM 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
				this.isPlaying = false;
			}
		}
	}
</script>
<style>
	.play-time-area {
		display: flex;
		flex-direction: row;
		margin-top: 20px;
	}

	.duration {
		margin-left: auto;
	}

	.play-button-area {
		display: flex;
		flex-direction: row;
		justify-content: center;
Anne_LXM's avatar
Anne_LXM 已提交
186
		margin: 50px 0;
Anne_LXM's avatar
Anne_LXM 已提交
187 188 189 190 191 192 193
	}

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