【发布时间】:2015-12-29 11:15:09
【问题描述】:
我在 iPhone 6 上使用 phonegap 播放 mp3 时遇到问题。
我正在从服务器下载一个 zip 文件,其中包含 1 个 mp3 文件和 3 个图像文件。
似乎都可以正确下载,但我不太确定 mp3 文件,因为它无法播放。
为了测试文件是否存在,我这样做:
<img src="cdvfile://localhost/persistent/audio/1.jpg" alt="" /><br>
<img src="cdvfile://localhost/persistent/audio/2.jpg" alt="" /><br>
<img src="cdvfile://localhost/persistent/audio/3.jpg" alt="" /><br>
<a href="cdvfile://localhost/persistent/audio/1.mp3">MP3HERE</a><br>
<audio class="audio" controls>
<source src="cdvfile://localhost/persistent/audio/1.mp3" type="audio/mpeg">
</audio>
显示了 3 张图片,所以我知道所有文件都必须在那里,但是虽然我得到了音频播放器,但它不会播放任何东西,所以我也为 mp3 创建了一个同样不播放的文件。
这是我在下面使用的函数:
function downloadAndUnzip() {
var fileTransfer = new FileTransfer();
fileTransfer.download(
"http://myserver.com/test.zip",
"cdvfile://localhost/persistent/audio/test.zip",
function(entry) {
alert("download complete: " + entry.fullPath);
zip.unzip("cdvfile://localhost/persistent/audio/test.zip",
"cdvfile://localhost/persistent/audio/",
function(){
alert('Zip decompressed successfully'); //I get this alert
alert('Checking if test.zip exists');
checkIfFileExists("/audio/test.zip"); //returns that it exists
alert('Checking if 1.jpg exists');
checkIfFileExists("/audio/1.jpg");
alert('Checking if 2.jpg exists');
checkIfFileExists("/audio/2.jpg"); //returns that it exists
alert('Checking if 3.jpg exists');
checkIfFileExists("/audio/3.jpg"); //returns that it exists
alert('Checking if 1en.mp3 exists');
checkIfFileExists("/audio/1.mp3"); //returns that it exists
}
);
},
function(error) {
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
});
}
现在,就警报而言,zip 文件已成功下载并解压缩。
关于如何解决或解决此问题的任何想法?
【问题讨论】:
-
您必须在 Content-Security-Poilcy 中启用媒体源。见我的帖子:stackoverflow.com/questions/34374139/…
-
我添加了这个: 但 mp3 仍然无法正常工作:/
-
只有当音频文件打包在应用程序中时我才能播放,但是当我下载音频文件时它们就无法播放
-
我对 mp3 被压缩的事实有点困惑。有什么好处?您是否测试过“干净”(解压缩)下载?
-
是的,我尝试了相同的代码,但没有图像、pdf 等的解压缩部分,它工作正常,但是当我对任何音频文件执行此操作时,它就无法播放。我需要这样做的原因是一个 zip 文件是我需要能够下载一个包含多个音频文件的 zip 文件。这是因为否则应用程序太大而无法提交到应用程序商店,所以我认为用户可以在安装应用程序后下载文件。希望这会有所帮助
标签: javascript cordova phonegap-build