简体   繁体   English

与MP3播放有关的Phonegap下载/解压缩问题

[英]Phonegap download / unzip issues with mp3 playing

I am having issues playing mp3s on my iphone 6 using phonegap. 我在使用phonegap在iphone 6上播放mp3时遇到问题。

I am downloading a zip file from the server which contains 1 mp3 file and 3 image files. 我正在从服务器下载一个zip文件,其中包含1个mp3文件和3个图像文件。

All seem to download correctly but I am not too sure about the mp3 file as it just wont play. 似乎所有人都可以正确下载,但是我不太确定mp3文件会不会播放。

To test if the files are there I am doing this: 要测试文件是否存在,我正在这样做:

<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>

The 3 images show up so I know all the files must be there but although I get the audio player it wont play anything so I created a like too to the mp3 which doesnt play either. 显示3张图片,所以我知道所有文件都必须在那儿,但是尽管我获得了音频播放器,但它不会播放任何东西,所以我也为mp3播放了一个类似的东西,但也无法播放。

Here is the function I used below: 这是我在下面使用的功能:

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);
    });

}

Now, as far as the alerts go, the zip file was successfully downloads and uncompressed. 现在,就警报而言,该zip文件已成功下载并解压缩。

Any ideas on how I can troubleshoot or fix this? 关于如何解决或解决此问题的任何想法?

I just ran into a similar problem, I wasn't able to show images from the local filesystem. 我只是遇到了类似的问题,我无法显示本地文件系统中的图像。

According to the docs at https://www.npmjs.com/package/cordova-plugin-file (under cdvfile protocol), I changed the Content Security rules: 根据https://www.npmjs.com/package/cordova-plugin-file (在cdvfile协议下)的文档,我更改了内容安全性规则:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap:cdvfile:https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

<access origin="cdvfile://*" />

Maybe it helps. 也许有帮助。

Update for clarification: 更新说明:

Put

<access origin="cdvfile://*" />

into the root config.xml file of your project under the widget. 到小部件下项目的根config.xml文件中。 It should look like this: 它看起来应该像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget ...>
...

 <access origin="cdvfile://*" />

...
</widget>

Put in your HTML the Content Security Rules like: 在您的HTML中放入内容安全规则,例如:

<meta http-equiv="Content-Security-Policy" content="
            default-src 'self' data: gap: cdvfile:

...

And then make the build with: 然后使用以下命令进行构建:

cordova build

Here is what worked in my case: 这是我的案例:

cdvfile:// did not work for me. cdvfile://对我不起作用。

It was all a problem with the path. 路径全都是问题。

In my case using "cordova.file.dataDirectory" fixed the problem. 就我而言,使用"cordova.file.dataDirectory"解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM