為了嘗試解決 Tone.js 庫和音頻緩沖區的另一個問題(請參見此處),我創建了一個“默認的 cordova hello world 應用程式”,并添加了“cordova-plugin-file”插件。
我不明白為什么,我可以處理一個遠程檔案并播放它,但是本地相同的檔案,我不能播放它,我錯了什么?
我從我的 index.html 中洗掉了所有“Content-Security-Policy”...我更改了 Config.xml 檔案中的權限,如下所示:
<preference name = "AndroidPersistentFileLocation" value = "Internal" />
<access origin = "cdvfile: //*" />
<access origin = "*" />
<allow-intent href = "cdvfile: //*/*" />
<allow-intent href = "http: //*/*" />
<allow-intent href = "https: //*/*" />
<allow-intent href = "tel: *" />
<allow-intent href = "sms: *" />
<allow-intent href = "mailto: *" />
<allow-intent href = "geo: *" />
然后在 deviceready 中,如果我切換到 Tone.js Player,外部 url 有效,本地 url 沒有,我錯在哪里?
//本地 NO ok!: "cdvfile://localhost/persistent/1_Hat.mp3"
//遠程OK!: “https://vivo-vivendo-musica.com/sample/1_Hat.mp3”
function onDeviceReady() {
// Cordova is now initialized. Have fun!
console.log('Running cordova-' cordova.platformId '@' cordova.version);
document.getElementById('deviceready').classList.add('ready');
//Local NO ok!: "cdvfile://localhost/persistent/1_Hat.mp3"
//Remote OK!: "https://vivo-vivendo-musica.com/sample/1_Hat.mp3"
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
console.log('file system open: ' fs.name);
fs.root.getFile("1_Hat.mp3", { exclusive: false }, function (fileEntry) {
console.log("fileEntry is file? " fileEntry.isFile.toString());
fileEntry.file(function (file) {
console.log(file.localURL);
player = new Tone.Player(file.localURL).toDestination();
// play as soon as the buffer is loaded
player.autostart = true;
});
}, console.log("getFile") );
}, console.log("onErrorLoadFs") );
}
我認為這幾乎可以肯定是一個安全問題,但我不明白缺少什么,我嘗試了各種方法來獲取這個檔案,它存在于 www 檔案夾中。
uj5u.com熱心網友回復:
最后我設法以這種方式解決,我不知道這是最好的方式,還是唯一的方式,但通過這種方式我可以獲取資源,存在于 www / 目錄中。這對我來說并不容易,我認為這可以幫助其他人。
//attach a click listener to a play button
document.querySelector('#Play').addEventListener('click', async () => {
await Tone.start()
console.log('audio is ready');
play();
})
function play() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "1_Hat.mp3", true);
xhr.responseType = 'blob';
xhr.onload = function(){
var blob = URL.createObjectURL(this.response);
console.log('pressed');
var player = new Tone.Player().toDestination();
player.load(blob);
player.autostart = true;
};
xhr.send();
}
謝謝你!很大的幫助:#628(評論)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412359.html
標籤:
