-
Notifications
You must be signed in to change notification settings - Fork 0
/
cordovaMedia2.js
127 lines (102 loc) · 3.01 KB
/
cordovaMedia2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* A rewrite of the Media class from ngCordova that properly reports media position and duration using Promises
*
* Author: Ariel Faur
* Version 1.0.2
* Date 07/06/2015
*/
// install : cordova plugin add org.apache.cordova.media
// link : https://github.com/apache/cordova-plugin-media
angular.module('ngCordova.plugins.media2', [])
.service('NewMedia', ['$q', '$interval', function ($q, $interval) {
var q, mediaStatus = null, mediaPosition = -1, mediaTimer, mediaDuration = -1;
function setTimer(media) {
if (angular.isDefined(mediaTimer)) return;
mediaTimer = $interval(function () {
if (mediaDuration < 0) {
mediaDuration = media.getDuration();
if (q && mediaDuration > 0) q.notify({duration: mediaDuration});
}
media.getCurrentPosition(
// success callback
function (position) {
if (position > -1) {
mediaPosition = position;
}
},
// error callback
function (e) {
console.log("Error getting pos=" + e);
});
if (q) q.notify({position: mediaPosition});
}, 1000);
}
function clearTimer() {
if (angular.isDefined(mediaTimer)) {
$interval.cancel(mediaTimer);
mediaTimer = undefined;
}
}
function resetValues() {
mediaPosition = -1;
mediaDuration = -1;
}
function NewMedia(src) {
this.media = new Media(src,
function (success) {
clearTimer();
resetValues();
q.resolve(success);
}, function (error) {
clearTimer();
resetValues();
q.reject(error);
}, function (status) {
mediaStatus = status;
q.notify({status: mediaStatus});
});
}
// iOS quirks :
// - myMedia.play({ numberOfLoops: 2 }) -> looping
// - myMedia.play({ playAudioWhenScreenIsLocked : false })
NewMedia.prototype.play = function (options) {
q = $q.defer();
if (typeof options !== "object") {
options = {};
}
this.media.play(options);
setTimer(this.media);
return q.promise;
};
NewMedia.prototype.pause = function () {
clearTimer();
this.media.pause();
};
NewMedia.prototype.stop = function () {
this.media.stop();
};
NewMedia.prototype.release = function () {
this.media.release();
this.media = undefined;
};
NewMedia.prototype.seekTo = function (timing) {
this.media.seekTo(timing);
};
NewMedia.prototype.setVolume = function (volume) {
this.media.setVolume(volume);
};
NewMedia.prototype.startRecord = function () {
this.media.startRecord();
};
NewMedia.prototype.stopRecord = function () {
this.media.stopRecord();
};
return NewMedia;
}])
.factory('$cordovaMedia2', ['NewMedia', function (NewMedia) {
return {
newMedia: function (src) {
return new NewMedia(src);
}
};
}]);