Skip to content

Instantly share code, notes, and snippets.

@rafabarzotto
Created February 17, 2017 22:37
Show Gist options
  • Save rafabarzotto/4cd38e2706796b4e3512c0d2c02d1a2a to your computer and use it in GitHub Desktop.
Save rafabarzotto/4cd38e2706796b4e3512c0d2c02d1a2a to your computer and use it in GitHub Desktop.
Angular Service ng-cordova media
app.service('AudioSvc', [function($ionicPopup) {
var AudioSvc = {
my_media: null,
mediaTimer: null,
playAudio: function(src, cb) {
var self = this;
// stop playing, if playing
self.stopAudio();
self.my_media = new Media(src, onSuccess, onError);
self.my_media.play();
if (self.mediaTimer == null) {
self.mediaTimer = setInterval(function() {
self.my_media.getCurrentPosition(
function(position) {
cb(position, self.my_media.getDuration());
},
function(e) {
console.log("Error getting pos=" + e);
}
);
}, 1000);
}
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
console.log("playAudio():Audio Error");
}
},
resumeAudio: function() {
var self = this;
if (self.my_media) {
self.my_media.play();
}
},
pauseAudio: function() {
var self = this;
if (self.my_media) {
self.my_media.pause();
}
},
stopAudio: function() {
var self = this;
if (self.my_media) {
self.my_media.stop();
}
if (self.mediaTimer) {
clearInterval(self.mediaTimer);
self.mediaTimer = null;
}
},
releaseAudio: function(){
var self = this;
if(self.my_media){
self.my_media.release();
}
}
};
return AudioSvc;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment