提交 f0cc4ff8 编写于 作者: M mahaifeng

[audio]添加音频示例

上级 615b79f6
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
<button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button> <button :disabled="isPlaying" type="primary" @click="play" class="uni-btn">播放</button>
<button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button> <button :disabled="!isPlaying" type="primary" @click="pause" class="uni-btn">暂停</button>
<button :disabled="!isPlaying && !isPaused" type="primary" @click="stop" class="uni-btn">停止</button> <button :disabled="!isPlaying && !isPaused" type="primary" @click="stop" class="uni-btn">停止</button>
<button type="primary" @click="onchange(20)" class="uni-btn">跳转到指定位置20</button> <button type="primary" @click="onchangeValue(20)" class="uni-btn">跳转到指定位置20</button>
<view class="uni-title"> <view class="uni-title">
<text class="uni-title-text">格式/路径示例</text> <text class="uni-title-text">格式/路径示例</text>
...@@ -87,24 +87,30 @@ ...@@ -87,24 +87,30 @@
this.isCanplay = true; this.isCanplay = true;
// 当音频可以播放时,获取缓冲信息 // 当音频可以播放时,获取缓冲信息
this.buffered = this._audioContext!.buffered; this.buffered = this._audioContext!.buffered;
this.duration = this._audioContext!.duration || 0; this.duration = this._audioContext!.duration
}); });
}, },
onchanging() { onchanging() {
this._isChanging = true; this._isChanging = true;
}, },
onchange(e) { onchange(e : UniSliderChangeEvent) {
console.log(e, 'e'); console.log(e, 'e');
let pos = typeof e === "number" ? e : e.detail.value; let pos = e.detail.value;
this._audioContext!.seek(pos); this._audioContext!.seek(pos);
this.onSeeking() this.onSeeking()
this.onSeeked() this.onSeeked()
this._isChanging = false; this._isChanging = false;
}, },
onchangeValue(pos:number) {
this._audioContext!.seek(pos);
this.onSeeking()
this.onSeeked()
this._isChanging = false;
},
startTimeInput(e : InputEvent) { startTimeInput(e : InputEvent) {
let startTimeValue = Number(e.detail.value) let startTimeValue = parseInt(e.detail.value)
this._audioContext!.startTime = startTimeValue; this._audioContext!.startTime = startTimeValue;
this.onchange(startTimeValue) this.onchangeValue(startTimeValue)
}, },
setAutoplay() { setAutoplay() {
this._audioContext!.autoplay = !this._audioContext!.autoplay; this._audioContext!.autoplay = !this._audioContext!.autoplay;
...@@ -125,7 +131,7 @@ ...@@ -125,7 +131,7 @@
this._audioContext!.play(); this._audioContext!.play();
this.isPlayEnd = false; this.isPlayEnd = false;
if (this._audioContext!.startTime > 0) { if (this._audioContext!.startTime > 0) {
this.onchange(this._audioContext!.startTime) this.onchangeValue(this._audioContext!.startTime)
} }
this._audioContext!.onPlay(() => { this._audioContext!.onPlay(() => {
this.isPaused = false; this.isPaused = false;
...@@ -158,7 +164,7 @@ ...@@ -158,7 +164,7 @@
this._audioContext!.onTimeUpdate(() => { this._audioContext!.onTimeUpdate(() => {
// console.log('onTimeUpdate:音频播放进度更新事件,currentTime',this._audioContext!.currentTime); // console.log('onTimeUpdate:音频播放进度更新事件,currentTime',this._audioContext!.currentTime);
if (this._isChanging === true) { return; } if (this._isChanging === true) { return; }
this.currentTime = this._audioContext!.currentTime || 0; this.currentTime = this._audioContext!.currentTime ?? 0;
console.log('currentTime', this.currentTime); console.log('currentTime', this.currentTime);
if (this.currentTime > this.buffered) { if (this.currentTime > this.buffered) {
console.log('缓冲不足'); console.log('缓冲不足');
......
<template> <template>
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt"> <!-- #ifdef APP -->
<view class="uni-title"> <scroll-view style="flex: 1;">
<text class="uni-title-text">支持的音频格式示例</text> <!-- #endif -->
</view> <view class="uni-padding-wrap uni-common-mt">
<view class="formats" v-for="(item,index) in supportFormats" :key="index"> <view class="uni-title">
<text class="uni-subtitle-text">{{item.format}}</text> <text class="uni-title-text">支持的音频格式示例</text>
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'" </view>
@click="play(item.src,index)"></image>
</view> <view class="formats" v-for="(item,index) in supportFormats" :key="index">
<text class="uni-subtitle-text">{{item.format}}</text>
<view class="uni-title"> <image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
<text class="uni-title-text">不支持的音频格式</text> @click="play(item.src,index)"></image>
</view> </view>
<view class="formats" v-for="(item,index) in notSupportFormats" :key="index">
<text class="uni-subtitle-text">{{item.format}}</text> <view class="uni-title">
<image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'" <text class="uni-title-text">不支持的音频格式</text>
@click="play(item.src,index)"></image> </view>
</view> <view class="formats" v-for="(item,index) in notSupportFormats" :key="index">
</view> <text class="uni-subtitle-text">{{item.format}}</text>
</template> <image class="icon-play" :src="(isPlaying && playIndex==index)?'/static/pause.png':'/static/play.png'"
@click="play(item.src,index)"></image>
<script> </view>
type AudioFormat = {
format : string </view>
src : string <!-- #ifdef APP -->
} </scroll-view>
export default { <!-- #endif -->
data() { </template>
return {
title: 'audio-format', <script>
playIndex: 0, type AudioFormat = {
isPlaying: false, format : string
_audioContext: null as InnerAudioContext | null, src : string
supportFormats: [ }
{ export default {
format: 'mp3', data() {
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3' return {
}, title: 'audio-format',
{ playIndex: 0,
format: 'mp4', isPlaying: false,
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4' _audioContext: null as InnerAudioContext | null,
}, supportFormats: [
{ {
format: 'm4a', format: 'mp3',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
}, },
{ {
format: 'aac', format: 'mp4',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp4'
}, },
{ {
format: 'flac', format: 'm4a',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.m4a'
}, },
{
{ format: 'aac',
format: 'ogg', src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg' },
}, {
{ format: 'flac',
format: 'wav', src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.flac'
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav' },
},
] as Array<AudioFormat>, {
notSupportFormats: [ format: 'ogg',
{ src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.ogg'
format: 'wma', },
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma' {
}, format: 'wav',
{ src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wav'
format: 'aiff', },
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff' ] as Array<AudioFormat>,
}, notSupportFormats: [
{ {
format: 'caf', format: 'wma',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wma'
}, },
{ {
format: '错误格式', format: 'aiff',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.aiff'
}, },
] as Array<AudioFormat> {
} format: 'caf',
}, src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.caf'
onReady() { },
this._audioContext = uni.createInnerAudioContext(); {
}, format: '错误格式',
onUnload() { src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.wmaa'
if (this._audioContext != null) { },
this.pause(); ] as Array<AudioFormat>
this._audioContext!.destroy() }
} },
}, onReady() {
methods: { this._audioContext = uni.createInnerAudioContext();
pause() { },
this._audioContext!.pause(); onUnload() {
this.isPlaying = false; if (this._audioContext != null) {
}, this.pause();
play(audioUrl, index) { this._audioContext!.destroy()
// console.log(index,audioUrl); }
if (this.isPlaying && this.playIndex == index) { },
this.pause(); methods: {
return; pause() {
} this._audioContext!.pause();
this.playIndex = index this.isPlaying = false;
this._audioContext!.src = audioUrl; },
this._audioContext!.play(); play(audioUrl : string, index : number) {
this.isPlaying = true; // console.log(index,audioUrl);
this._audioContext!.onPlay(() => { if (this.isPlaying && this.playIndex == index) {
console.log('开始播放'); this.pause();
}); return;
this._audioContext!.onEnded(() => { }
console.log('播放结束'); this.playIndex = index
this.isPlaying = false; this._audioContext!.src = audioUrl;
}); this._audioContext!.play();
this._audioContext!.onError((err) => { this.isPlaying = true;
this.isPlaying = false; this._audioContext!.onPlay(() => {
console.log('err', err); console.log('开始播放');
}); });
}, this._audioContext!.onEnded(() => {
}, console.log('播放结束');
} this.isPlaying = false;
</script> });
this._audioContext!.onError((err) => {
<style> this.isPlaying = false;
.formats { console.log('err', err);
align-items: center; });
} },
},
.icon-play { }
width: 60px; </script>
height: 60px;
margin: 10px; <style>
} .formats {
</style> align-items: center;
}
.icon-play {
width: 60px;
height: 60px;
margin: 10px;
}
</style>
\ No newline at end of file
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
description: '本地路径:/static方式', description: '本地路径:/static方式',
src: '/static/test-audio/ForElise.mp3' src: '/static/test-audio/ForElise.mp3'
}, },
{ {
description: '本地路径:../static/', description: '本地路径:../static/',
src: '../../../static/test-audio/ForElise.mp3' src: '../../../static/test-audio/ForElise.mp3'
}, },
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
this._audioContext!.pause(); this._audioContext!.pause();
this.isPlaying = false; this.isPlaying = false;
}, },
play(audioUrl, index) { play(audioUrl:string, index:number) {
// console.log(index,audioUrl); // console.log(index,audioUrl);
if (this.isPlaying && this.playIndex == index) { if (this.isPlaying && this.playIndex == index) {
this.pause(); this.pause();
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
}); });
this._audioContext!.onError((err) => { this._audioContext!.onError((err) => {
this.isPlaying = false; this.isPlaying = false;
console.log('err', err); console.log('err', err);
}); });
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册