如何下载Angularjs中存储在http服务器中的音乐

问题描述 投票:1回答:2

假设我有一个存储一些.mp3音乐的API。示例链接在这里:

https://118.69.201.34:8882/api/ApiMusic/Download?songId=2000

现在我想在Angularjs中编写一个API调用函数,将音乐下载到我的Android设备,其中包含歌曲的Id编号,如链接中所示。

我怎样才能做到这一点?请帮忙 :(

android ios angularjs ionic
2个回答
1
投票

您可以在此处使用ngCordova FileTransfer库:http://ngcordova.com/docs/plugins/fileTransfer/

以下是该页面的示例代码,调整到您的示例网址:

document.addEventListener('deviceready', function () {

var fileid = "2000";
var url = "https://118.69.201.34:8882/api/ApiMusic/Download?songId=" + fileid;
var targetPath = cordova.file.documentsDirectory + fileid + ".mp3";
var trustHosts = true
var options = {};

$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
  .then(function(result) {
    // Success!
  }, function(err) {
    // Error
  }, function (progress) {
    $timeout(function () {
      $scope.downloadProgress = (progress.loaded / progress.total) * 100;
    })
  });
}, false);

0
投票

我终于做到了,这是我的代码。只为那些想要在未来引用此问题的人分享。谢谢你们的回答

$scope.download = function(songId, songName) {
    $ionicLoading.show({
      template: 'Downloading...'
    });
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
          fs.root.getDirectory(
              "fMusic",
              {
                create: true
              },
              function (dirEntry) {
                dirEntry.getFile(
                    songName + ".mp3",
                    {
                      create: true,
                      exclusive: false
                    },
                    function gotFileEntry(fe) {
                      var p = fe.toURL();
                      fe.remove();
                      ft = new FileTransfer();
                      ft.download(
                          encodeURI(APIUrl + songId),
                          p,
                          function (entry) {
                            $ionicLoading.hide();
                            $scope.mp3File = entry.toURL();
                          },
                          function (error) {
                            $ionicLoading.hide();
                            alert("Download Error Source --> " + error.source);
                          },
                          false,
                          null
                      );
                    },
                    function () {
                      $ionicLoading.hide();
                      console.log("Get the file failed");
                    }
                );
              }
          );
        },
        function () {
          $ionicLoading.hide();
          console.log("Request for filesystem failed");
        });
  }
© www.soinside.com 2019 - 2024. All rights reserved.