如何在离子中播放外部音频文件

时间:2015-01-23 16:03:16

标签: javascript html css

我是离子线索的新手,我在尝试播放音频文件时遇到了一些问题,例如。我的应用程序中的mp3。该平台是android。我已经完成了角度js文档,但我似乎变得更加困惑。

这是我的控制器



.controller('AudioCtrl', function($scope, $cordovaMedia) {

  var src = "/android_asset/www/audio/gingle.mp3";
  var media = $cordovaMedia.newMedia(src).then(function() {
    // success
  }, function () {
    // error
  });

  $scope.playAudio = function(){
    media.play(); 
  }
  $scope.stopAudio = function(){
    media.stop();
  }
  $scope.pauseAudio = function(){
     media.pause();
  }
});




这个播放音频的页面



<ion-view title="Audio Message">
    <ion-content class="has-header padding" ng-controller="AudioCtrl">

        
        <button ng-click="playAudio()">Play Audio</button>
        <button ng-click="stopAudio()">stop Audio</button>
         <button ng-click="pauseAudio()">pause Audio</button>

    </ion-content>
</ion-view>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您是否考虑过使用ngCordova插件?

他们有原生音频:

cordova plugin add https://github.com/SidneyS/cordova-plugin-nativeaudio.git

您可以像这样使用它:

module.controller('MyCtrl', function($scope, $cordovaNativeAudio, $timeout) {

  $cordovaNativeAudio
    .preloadSimple('click', 'audio/click.mp3')
    .then(function (msg) {
      console.log(msg);
    }, function (error) {
      alert(error);
    });

  $cordovaNativeAudio
    .preloadComplex('music', 'audio/music.mp3', 1, 1)
    .then(function (msg) {
      console.log(msg);
    }, function (error) {
      console.error(error);
    });

  $scope.play = function () {
    $cordovaNativeAudio.play('click');
    $cordovaNativeAudio.loop('music');

    // stop 'music' loop and unload
    $timeout(function () {
      $cordovaNativeAudio.stop('music');

      $cordovaNativeAudio.unload('click');
      $cordovaNativeAudio.unload('music');
    }, 1000 * 60);
  };

});