driftyco/ng-cordova — pull request #427 by mawarnes (2014-11-10 13:13:23+00:00)
Title: Update media.js

brilliant project, great work!

it's taken me most of the weekend to get to the bottom of this but I'd like to propose a less promisey way to implement the media functionality.

but you know after some reflection, it would 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.

additionally the play, pause, stop etc... operations will now work as intended.

therefore you might want to change the constructor to the following

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();
}
};