使用ngcordova下载后如何播放mp3文件

时间:2014-12-30 15:44:32

标签: ios cordova ionic

我制作一个cordova应用程序从服务器下载mp3文件然后存储到手机中,然后用户可以在本地播放。

我下载mp3文件的代码看起来像这样

$scope.downloadFile = function(Filename){
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("http://archive.org/download/Kansas_Joe_Memphis_Minnie-When_Levee_Breaks/Kansas_Joe_and_Memphis_Minnie-When_the_Levee_Breaks.mp3");
        var downloadPath = cordova.file.dataDirectory+'download.mp3';

        fileTransfer.download(
            uri,
            downloadPath,
            function(entry) {
                $scope.savepath = entry.toURL();
                alert("download complete: " + entry.toURL());
            },
            function(error) {
                alert("download error");
            },
            false,
            {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
        );
    }//End DownloadFile

我从服务器下载mp3文件,并通过名称download.mp3将其存储在iPhone中。当我想播放它时,我使用文件路径cordova.file.dataDirectory+'download.mp3',但是有一个错误,就像应用无法找到download.mp3

这是来自xCode的日志

Unknown resource 'file:///var/mobile/Containers/Data/Application/F4G316AE-5BB2-3F1A-A65B-A84E3F7152AC/Library/NoCloud/download.mp3'

我想问你们,有没有办法检查正确的文件路径或任何解决这个问题的想法。

问候。

1 个答案:

答案 0 :(得分:5)

您需要取消"文件://"从文件路径

fileURL = fileURL.replace('file://','');

$scope.media = new Media(fileURL);

$cordovaMedia.play($scope.media);

/*
'file:///var/mobile/Containers/Data/Application/XXXXXXXXX/Library/NoCloud/download.mp3'
--> Unknown resource

'/var/mobile/Containers/Data/Application/XXXXXXXXX/Library/NoCloud/download.mp3'
--> play ok!
*/