This repository was archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmedia.js
More file actions
140 lines (114 loc) · 3.27 KB
/
media.js
File metadata and controls
140 lines (114 loc) · 3.27 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
// install : cordova plugin add cordova-plugin-media
// link : https://github.com/apache/cordova-plugin-media
/* globals Media: true */
angular.module('ngCordova.plugins.media', [])
.service('NewMedia', ['$q', '$interval', function ($q, $interval) {
var q, q2, q3, 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();
};
NewMedia.prototype.currentTime = function () {
q2 = $q.defer();
this.media.getCurrentPosition(function (position){
q2.resolve(position);
});
return q2.promise;
};
NewMedia.prototype.getDuration = function () {
q3 = $q.defer();
q3.resolve(this.media.getDuration());
return q3.promise;
};
return NewMedia;
}])
.factory('$cordovaMedia', ['NewMedia', function (NewMedia) {
return {
newMedia: function (src) {
return new NewMedia(src);
}
};
}]);