From 8e9b079df3afe49fb3e3929e3b8cd2624ab6129f Mon Sep 17 00:00:00 2001 From: zhaofengliang Date: Tue, 26 Nov 2024 16:31:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20inner-audio-path=20?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inner-audio-path.uvue | 108 ++++++++++-------- 1 file changed, 61 insertions(+), 47 deletions(-) diff --git a/pages/API/create-inner-audio-context/inner-audio-path.uvue b/pages/API/create-inner-audio-context/inner-audio-path.uvue index 42d12843..b7434452 100644 --- a/pages/API/create-inner-audio-context/inner-audio-path.uvue +++ b/pages/API/create-inner-audio-context/inner-audio-path.uvue @@ -22,23 +22,23 @@ return { title: 'audio-path', playIndex: 0, - isPlaying: false, - nativePath:uni.env.CACHE_PATH+'uni-audio/test/test.mp3' as string, - sdcardPath :'sdcard/uni-audio/test.mp3', + 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, supportPaths: [ { description: '本地路径:/static方式', src: '/static/test-audio/ForElise.mp3' }, - { + { description: '本地路径:../static/', src: '../../../static/test-audio/ForElise.mp3' - }, - { - description: '本地路径:env方式', - src: 'env' - }, + }, + { + description: '本地路径:env方式', + src: 'env' + }, { description: '网络路径', src: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/audio/ForElise.mp3' @@ -55,37 +55,51 @@ } }, onReady() { - this._audioContext = uni.createInnerAudioContext(); - this._audioContext!.onPlay(() => { - console.log('开始播放'); - }); - this._audioContext!.onEnded(() => { - console.log('播放结束'); - this.isPlaying = false; - }); - this._audioContext!.onError((err) => { - this.isPlaying = false; - 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){ - } - }, - + this._audioContext = uni.createInnerAudioContext(); + this._audioContext!.onPlay(() => { + console.log('开始播放'); + }); + this._audioContext!.onEnded(() => { + console.log('播放结束'); + this.isPlaying = false; + }); + this._audioContext!.onError((err) => { + this.isPlaying = false; + console.log('err', err); + }); + + const fileManager = uni.getFileSystemManager() + fileManager.mkdir({ + dirPath: uni.env.CACHE_PATH + 'uni-audio/test', + recursive: true, + success: (res) => { + fileManager.copyFile({ + srcPath: '/static/test-audio/ForElise.mp3', + destPath: this.nativePath, + success: () => { + console.log("copy成功: ", res) + } + }) + }, + fail: (err) => { + 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() { if (this._audioContext != null) { this.pause(); @@ -97,16 +111,16 @@ this._audioContext!.pause(); this.isPlaying = false; }, - play(audioUrl:string, index:number) { - console.log(index,audioUrl); + play(audioUrl : string, index : number) { + console.log(index, audioUrl); if (this.isPlaying && this.playIndex == index) { this.pause(); return; - } - if(audioUrl == 'env'){ - audioUrl=this.nativePath - } - + } + if (audioUrl == 'env') { + audioUrl = this.nativePath + } + this.playIndex = index this._audioContext!.src = audioUrl; this._audioContext!.play(); -- GitLab