it’s taken me most of the weekend to get to the bottom of this!! just for info. it looks like the ngCordova implementation might have a few problems
after careful consideration, it might be better to change the constructor to a non-promisey one, but only make certain functions…like getDuration promisey. The reasoning here is that the media callbacks need to fire every time the file is played to allow ui updates, for example the play button could change to a pause button. If you make the constructor promisey it will only fire these callbacks the first time the file is played.
therefore you might want to change the constructor to the following in ng-cordova.js
newMedia: function (src,success,error,mediaStatus) {
var media = new Media(src,success,error,mediaStatus);
return media;
},
so the finished module would be something like
angular.module('ngCordova.plugins.media', [])
.factory('$cordovaMedia', ['$q', function ($q) {
return {
newMedia: function (src,success,error,mediaStatus) {
var media = new Media(src,success,error,mediaStatus);
return media;
},
getCurrentPosition: function (source) {
var q = $q.defer();
source.getCurrentPosition(function (success) {
q.resolve(success);
}, function (error) {
q.reject(error);
});
return q.promise;
},
getDuration: function (source) {
return source.getDuration();
},
play: function (source) {
source.play();
// iOS quirks :
// - myMedia.play({ numberOfLoops: 2 }) -> looping
// - myMedia.play({ playAudioWhenScreenIsLocked : false })
},
pause: function (source) {
return source.pause();
},
release: function (source) {
return source.release();
},
seekTo: function (source, milliseconds) {
return source.seekTo(milliseconds);
},
setVolume: function (source, volume) {
return source.setVolume(volume);
},
startRecord: function (source) {
return source.startRecord();
},
stopRecord: function (source) {
return source.stopRecord();
},
stop: function (source) {
return source.stop();
}
};
you coud then play the file with this kind of code
$ionicPlatform.ready(
function () {
var filestring='/android_asset/www/gong.mp3';
$scope.thisMedia = new $cordovaMedia.newMedia(filestring,
//fires after the file has played
function(success){
alert('success');
},
function(error){
alert(err);
},
//fires when media status changes
function(ms){
alert(ms);
}
);
$scope.mediaStuff=$scope.thisMedia;
}
);
$scope.playMedia = function () {
$cordovaMedia.play($scope.thisMedia);
};
$scope.stopMedia = function () {
$cordovaMedia.stop($scope.thisMedia);
};
$scope.pauseMedia = function () {
$cordovaMedia.pause($scope.thisMedia);
};