Cordova,我可以远程使用文件,但不能在本地使用,我错在哪里



为了解决Tone.js库和音频缓冲区的另一个问题(请参阅此处(,我创建了一个;默认cordova hello world应用程序";,并添加";cordova插件文件";插件。

我不明白为什么,我可以处理一个远程文件并播放它,但是同一个文件在本地,我不能播放,我怎么了?。

我删除了所有的";内容安全策略";从我的index.html…我更改了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">

//远程正常!:";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文件夹中。

最后我设法用这种方式解决了问题,我不知道这是最好的方法,还是唯一的方法,但通过这种方式,我可以访问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(评论(

相关内容

最新更新