Cordova,我可以远程使用文件但不能在本地使用,我错了什么?
Cordova, I can use a file remotely but not locally, what am I wrong?
尝试解决 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 播放器,外部 url 工作,本地 url 不,我哪里错了?
//本地不行!: "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 文件夹中的文件。
最后我设法用这种方式解决了,我不知道这是最好的方式,还是唯一的方式,但这样我可以得到资源,目前在 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 (comment)
尝试解决 Tone.js 库和音频缓冲区的另一个问题(
我不明白为什么,我可以处理一个远程文件并播放它, 但是本地同一个文件,我不能播放,我哪里错了?。
我从 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 播放器,外部 url 工作,本地 url 不,我哪里错了?
//本地不行!: "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 文件夹中的文件。
最后我设法用这种方式解决了,我不知道这是最好的方式,还是唯一的方式,但这样我可以得到资源,目前在 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 (comment)