ReferenceError:未定义媒体

时间:2016-11-04 08:34:38

标签: angularjs ionic-framework

我试图让媒体播放器使用离子angularjs,所有这些都做得最多但是当运行应用程序并点击播放时出错ReferenceError: media is not defined有没有人有建议!我还使用了API文档中的媒体播放器的完整示例。我的代码目前看起来像这样:

angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('ctrl',function($scope,$cordovaMedia,$cordovaFile,$ionicLoading){


$scope.data = new Object();
var src = "sweet.mp3";
//var media = new Media(src, null, null, mediaStatusCallback);


 $scope.init = function() {
  var media = new Media(src, null, null, mediaStatusCallback);
 };
  //var media = new Media(src, null, null, mediaStatusCallback);
  $scope.play = function(){
   media.play();
      }
      // Update media position every second
          $scope.mediaTimer = setInterval(function () {
          // get media position
          media.getCurrentPosition(
         // success callback
         function (position) {
            if (position > -1) {
                console.log((position) + " sec");
                 $scope.adjust = position;
            }
         },
         // error callback
         function (e) {
              console.log("Error getting pos=" + e);
         }
        );
        }, 1000); //calling mediaTimer function in every 1 second

   $scope.stop = function(){
  console.log("stop");
  media.stop();
}
$scope.pause = function(){
  media.pause();
}
$scope.seek = function(){
  console.log("Seek to, duration :" + media._duration); // media._duration = length of song (in seconds)
  var time = media._duration * 1000; // time = length of song (in milliseconds) 
  var adjustingSong = (time/100) * $scope.adjust;  // algo to get relation btw rang and length of any song

  media.seekTo(adjustingSong);
}

$scope.duration = function(){
  console.log("duration" + media._duration);
//media.getDuration(media);
//console.log("time : "+ JSON.stringify(media));


}


//$scope.song();

var mediaStatusCallback = function(status) {
  console.log("media status call back called.!");
    if(status == 1){
      $ionicLoading.show({template: 'Loading...'});
    } else {
      $ionicLoading.hide();
    }
}

});

然而,当我玩的时候它不起作用,谢谢

1 个答案:

答案 0 :(得分:0)

.controller('ctrl',function($scope,$cordovaMedia,$cordovaFile,$ionicLoading){


$scope.data = new Object();
var src = "sweet.mp3";
var media; //Declare variable here so it's usable in controller functions

init(); //Initialize media
//$scope.play can now be called without `ReferenceError`
function init() {
 media = new Media(src, null, null, mediaStatusCallback);
};

2个问题:

  • 您实际上并未在代码中的任何位置调用init,这是实例化media的代码块。另外,如果您未在模板中实际使用它(通常是初始化函数的情况),则不必将其放在$scope上。

  • 变量是功能范围的,所以如果你不在media函数之外声明init,它实际上不会在它之外可用。