提交 b6a669a0 编写于 作者: M mahaifeng

[audio]添加音频路径env示例与多音频同时播放示例

上级 c0778ac1
...@@ -1125,6 +1125,12 @@ ...@@ -1125,6 +1125,12 @@
"navigationBarTitleText": "inner-audio-path" "navigationBarTitleText": "inner-audio-path"
} }
}, },
{
"path": "pages/API/create-inner-audio-context/inner-audio-mult",
"style": {
"navigationBarTitleText": "inner-audio-mult"
}
},
// #endif // #endif
// #ifdef WEB || MP // #ifdef WEB || MP
{ {
......
...@@ -63,6 +63,9 @@ ...@@ -63,6 +63,9 @@
<navigator url="/pages/API/create-inner-audio-context/inner-audio-path" class="uni-btn uni-common-mb"> <navigator url="/pages/API/create-inner-audio-context/inner-audio-path" class="uni-btn uni-common-mb">
<button type="primary" @click="pause">音频路径示例</button> <button type="primary" @click="pause">音频路径示例</button>
</navigator> </navigator>
<navigator url="/pages/API/create-inner-audio-context/inner-audio-mult" class="uni-btn uni-common-mb">
<button type="primary" @click="pause">同时播放</button>
</navigator>
</view> </view>
<!-- #ifdef APP --> <!-- #ifdef APP -->
</scroll-view> </scroll-view>
......
<template>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap uni-common-mt">
<view class="uni-title">
<text class="uni-title-text">多音频同时播放</text>
</view>
<button type="primary" @tap="play1()"> 播放/停止(进度:{{currentTime1}})</button>
<button type="primary" @tap="play2()"> 播放/停止(进度:{{currentTime2}})</button>
</view>
</template>
<script>
type AudioPath = {
description : string
src : string
}
export default {
data() {
return {
title: "多音频同时播放",
_audioContext1: null as InnerAudioContext | null,
src: 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3',
_audioContext2: null as InnerAudioContext | null,
playing1: false,
playing2: false,
currentTime1: 0,
currentTime2: 0,
}
},
onReady() {
this._audioContext1 = uni.createInnerAudioContext();
this._audioContext1!.src = this.src
this._audioContext1!.onTimeUpdate((res) => {
this.currentTime1 = this._audioContext1!.currentTime;
})
this._audioContext2 = uni.createInnerAudioContext();
this._audioContext2!.src = this.src
this._audioContext2!.onTimeUpdate((res) => {
this.currentTime2 = this._audioContext2!.currentTime;
})
},
onUnload() {
if (this._audioContext1 != null) {
this._audioContext1!.stop()
this._audioContext1!.destroy()
}
if (this._audioContext2 != null) {
this._audioContext2!.stop()
this._audioContext2!.destroy()
}
},
methods: {
play1() {
if (this._audioContext1 != null) {
this.currentTime1=0
if (this.playing1) {
this._audioContext1!.stop()
} else {
this._audioContext1!.play()
}
}
this.playing1 = !this.playing1
},
play2() {
if (this._audioContext2 != null) {
this.currentTime2=0
if (this.playing2) {
this._audioContext2!.stop()
} else {
this._audioContext2!.play()
}
}
this.playing2 = !this.playing2
}
}
}
</script>
<style>
.formats {
align-items: center;
}
.icon-play {
width: 60px;
height: 60px;
margin: 10px;
}
</style>
\ No newline at end of file
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
return { return {
title: 'audio-path', title: 'audio-path',
playIndex: 0, playIndex: 0,
isPlaying: false, isPlaying: false,
nativePath:uni.env.CACHE_PATH+'uni-audio/test/test.mp3' as string,
sdcardPath :'sdcard/uni-audio/test.mp3',
_audioContext: null as InnerAudioContext | null, _audioContext: null as InnerAudioContext | null,
supportPaths: [ supportPaths: [
{ {
...@@ -32,7 +34,11 @@ ...@@ -32,7 +34,11 @@
{ {
description: '本地路径:../static/', description: '本地路径:../static/',
src: '../../../static/test-audio/ForElise.mp3' src: '../../../static/test-audio/ForElise.mp3'
}, },
{
description: '本地路径:env方式',
src: 'env'
},
{ {
description: '网络路径', description: '网络路径',
src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3' src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3'
...@@ -60,8 +66,26 @@ ...@@ -60,8 +66,26 @@
this._audioContext!.onError((err) => { this._audioContext!.onError((err) => {
this.isPlaying = false; this.isPlaying = false;
console.log('err', err); console.log('err', err);
}); });
},
const fileManager = uni.getFileSystemManager()
try {
fileManager.rmdirSync(uni.env.CACHE_PATH+'uni-audio/test',true)
} catch (e) {
}
try {
fileManager.mkdirSync(uni.env.CACHE_PATH+'uni-audio/test',true)
} catch (e) {
}
try{
fileManager.copyFileSync(
'/static/test-audio/ForElise.mp3',
this.nativePath)
} catch(e){
}
},
onUnload() { onUnload() {
if (this._audioContext != null) { if (this._audioContext != null) {
this.pause(); this.pause();
...@@ -78,7 +102,11 @@ ...@@ -78,7 +102,11 @@
if (this.isPlaying && this.playIndex == index) { if (this.isPlaying && this.playIndex == index) {
this.pause(); this.pause();
return; return;
} }
if(audioUrl == 'env'){
audioUrl=this.nativePath
}
this.playIndex = index this.playIndex = index
this._audioContext!.src = audioUrl; this._audioContext!.src = audioUrl;
this._audioContext!.play(); this._audioContext!.play();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册