提交 8e9b079d 编写于 作者: zhaofengliang920817's avatar zhaofengliang920817

修改 inner-audio-path 示例。

上级 1bd26566
...@@ -22,23 +22,23 @@ ...@@ -22,23 +22,23 @@
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, nativePath: uni.env.CACHE_PATH + 'uni-audio/test/test.mp3' as string,
sdcardPath :'sdcard/uni-audio/test.mp3', sdcardPath: 'sdcard/uni-audio/test.mp3',
_audioContext: null as InnerAudioContext | null, _audioContext: null as InnerAudioContext | null,
supportPaths: [ supportPaths: [
{ {
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'
}, },
{ {
description: '本地路径:env方式', description: '本地路径:env方式',
src: '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'
...@@ -55,37 +55,51 @@ ...@@ -55,37 +55,51 @@
} }
}, },
onReady() { onReady() {
this._audioContext = uni.createInnerAudioContext(); this._audioContext = uni.createInnerAudioContext();
this._audioContext!.onPlay(() => { this._audioContext!.onPlay(() => {
console.log('开始播放'); console.log('开始播放');
}); });
this._audioContext!.onEnded(() => { this._audioContext!.onEnded(() => {
console.log('播放结束'); console.log('播放结束');
this.isPlaying = false; this.isPlaying = false;
}); });
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() const fileManager = uni.getFileSystemManager()
try { fileManager.mkdir({
fileManager.rmdirSync(uni.env.CACHE_PATH+'uni-audio/test',true) dirPath: uni.env.CACHE_PATH + 'uni-audio/test',
} catch (e) { recursive: true,
} success: (res) => {
fileManager.copyFile({
try { srcPath: '/static/test-audio/ForElise.mp3',
fileManager.mkdirSync(uni.env.CACHE_PATH+'uni-audio/test',true) destPath: this.nativePath,
} catch (e) { success: () => {
} console.log("copy成功: ", res)
try{ }
fileManager.copyFileSync( })
'/static/test-audio/ForElise.mp3', },
this.nativePath) fail: (err) => {
} catch(e){ console.log("创建路径失败: ", err.errMsg)
} if (err.errMsg.includes("file already exists")) {
}, console.log("已经包含该路径")
fileManager.copyFile({
srcPath: '/static/test-audio/ForElise.mp3',
destPath: this.nativePath,
success: (res) => {
console.log("copy成功: ", res)
},
fail: (err) => {
console.log("copy失败: ", err)
}
})
}
}
})
},
onUnload() { onUnload() {
if (this._audioContext != null) { if (this._audioContext != null) {
this.pause(); this.pause();
...@@ -97,16 +111,16 @@ ...@@ -97,16 +111,16 @@
this._audioContext!.pause(); this._audioContext!.pause();
this.isPlaying = false; this.isPlaying = false;
}, },
play(audioUrl:string, index:number) { 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();
return; return;
} }
if(audioUrl == 'env'){ if (audioUrl == 'env') {
audioUrl=this.nativePath 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.
先完成此消息的编辑!
想要评论请 注册