简体   繁体   中英

ng cordova media plugin stop method not working

iam created simple ionic audio app .its play function is work correctly but stop function not working.this is the code please help to fix this this is controller

 angular.module('starter', ['ionic', 'ngCordova']) .controller("ExampleController", function($scope, $ionicPlatform, $cordovaMedia, $ionicLoading) { $ionicPlatform.ready(function() { $scope.play = function() { var media = new Media(src, null, null, mediaStatusCallback); $cordovaMedia.play(media); }; $scope.pause = function() { media.pause(); }; $scope.stop = function() { media.stop(); }; var mediaStatusCallback = function(status) { if(status == 1) { $ionicLoading.show({template: 'Loading...'}); } else { $ionicLoading.hide(); } } }); }) 

and this is the html code :

  <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content> <button class="button" ng-click="play('http://www.stephaniequinn.com/Music/Commercial%20DEMO%20-%2013.mp3')">Play from internet</button> <button class="button" ng-click="stop()">Stop</button> </ion-content> </ion-pane> 

Your media variable is local . So in stop function media is undefined or not the same.

Moreover why are you use methods in different ways? I think you should use the same (eg media.play() or $cordovaMedia.stop(media); if it's possible)

Your variable var media is out of the scope of $scope.pause and $scope.stop. Try declaring it outside on the $scope.play function like so..

 angular.module('starter', ['ionic', 'ngCordova']) .controller("ExampleController", function($scope, $ionicPlatform, $cordovaMedia, $ionicLoading) { $ionicPlatform.ready(function() { var media = new Media(src, null, null, mediaStatusCallback); $scope.play = function() { $cordovaMedia.play(media); }; $scope.pause = function() { media.pause(); }; $scope.stop = function() { media.stop(); }; var mediaStatusCallback = function(status) { if(status == 1) { $ionicLoading.show({template: 'Loading...'}); } else { $ionicLoading.hide(); } } }); }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM