I'm trying to create a wrapper / factory around the cordova media plugin, which requires you to create a new Media object, and then use it again to pause, stop, play, etc it.
The factory + link to actual code:
.factory('$cordovaMedia', ['$q', function ($q) {
return {
newMedia: function (src) {
var q = $q.defer();
var mediaStatus = null;
var media = new Media(src,
function (success) {
q.resolve(success);
}, function (error) {
q.reject(error);
}, function (status) {
mediaStatus = status;
});
return {
media: media,
mediaStatus: mediaStatus,
promise: q.promise
}
},
play: function (source) {
return source.play();
},
stop: function (source) {
return source.play();
}
}]);
Example usage of how the factory works with this implementation:
var media = $cordovaMedia.newMedia('some/src.mp3').media
$cordovaMedia.play(media);
$cordovaMedia.stop(media);
As you can see I'm creating a new Media
object, and assigning it to a variable and using it again through the factory.
I'm wondering if there is a better way of doing this? Mainly what is the best way in AngularJS to create an object in a factory, which must be used again, through the same factory?