ionic使用cordovaMedia、cordovaFile和cordovaFileTransfer播放在线音乐

版权声明:本文为博主gzgchzc原创文章,转载必须注明出处。http://blog.gzvtc.cc/ https://blog.csdn.net/qq_33561700/article/details/53841118

gzgchzc博客最新更新地址:http://blog.gzvtc.cc/

首先安装cordovaNativeAudio和cordovaFileTransfer插件

cordova plugin add cordova-plugin-media
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-file-transfer

配置config.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility"/>

js代码如下

.controller('AudioInterNetCtrl', function ($scope, $cordovaMedia, $cordovaFileTransfer,$ionicPlatform,$cordovaFile,$timeout) {
$scope.params={url:''};
    $scope.play = function () {
      var fileName=new Date().getTime();
      document.addEventListener('deviceready', function () {

        var url = $scope.params.url;

        var targetPath = cordova.file.cacheDirectory + fileName+".mp3";
        var trustHosts = true;
        var options = {};

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

html代码如下

<ion-view view-title="audioInterNet">
  <ion-content>
<textarea ng-model="params.url" style="width: 100%;height: 50%"></textarea>
    <p>{{downloadProgress}}</p>
    <button ng-click="play()">点击播放在线音乐</button>
  </ion-content>
</ion-view>
阅读更多

没有更多推荐了,返回首页