-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (116 loc) · 2.76 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var URL = 'http://localhost:10001/mobile.landr.app/demo_full/1_original.mp3';
if (URL === null) {
throw 'Please set `URL` property in index.js';
}
var AUDIO_EVENTS = [
//Loading events
'loadstart',
'durationchange',
'loadedmetadata',
'loadeddata',
'progress',
'canplay',
'canplaythrough',
//Interruption events
'suspend',
'abort',
'error',
'emptied',
'stalled',
//Playing events
'timeupdate',
'playing',
'waiting',
'play',
'pause',
'ended',
'volumechange',
//Buffering events
'seeking',
'seeked'
];
var Player = function(media) {
this.media = media;
this.logs = [];
for (var i = 0; i < AUDIO_EVENTS.length; i++) {
this.listenTo(AUDIO_EVENTS[i]);
}
};
Player.prototype.play = function() {
this._log('action', 'play');
this.media.play();
};
Player.prototype.pause = function() {
this._log('action', 'pause');
this.media.pause();
};
Player.prototype.seek = function(ratio) {
var time = ratio * this.media.duration;
this._log('action', 'seek', 'Seeking to ' + time);
this.media.currentTime = time;
};
Player.prototype.listenTo = function(eventName) {
this.media.addEventListener(eventName, function() {
this._log('event', eventName);
}.bind(this));
};
Player.prototype.getMediaStatus = function() {
return {
currentTime: this.media.currentTime,
duration: this.media.duration,
ended: this.media.ended,
error: this.media.error,
paused: this.media.paused,
played: this._timeRange(this.media.played), //TimeRange
buffered: this._timeRange(this.media.buffered), //TimeRange
seekable: this._timeRange(this.media.seekable), //TimeRange
seeking: this.media.seeking,
}
};
Player.prototype.resetLogs = function() {
this.logs = [];
};
Player.prototype.getLogs = function() {
return this.logs;
};
Player.prototype._log = function(type, name, msg) {
var item = {
type: type,
name: name,
time: (new Date()).toISOString(),
media: this.getMediaStatus()
};
if (msg) {
item.message = msg;
}
this.logs.push(item);
console.log(item);
};
Player.prototype._timeRange = function(timeRange) {
var result = [];
for (var i = 0; i < timeRange.length; i++) {
result.push({
position: i,
start: timeRange.start(i),
end: timeRange.end(i)
});
}
return result;
};
$(document).ready(function() {
$('#player').html('<source src="' + URL + '?v=' + (new Date()).getTime() + '"></source>');
var player = new Player(document.getElementById('player'));
console.log(player);
$('#seek').on('click', function() {
player.seek(0.6);
// player.play();
});
$('#play').on('click', function() {
player.play();
});
$('#log').on('click', function() {
var logs = JSON.stringify(player.getLogs());
console.log(logs);
$('#logs').val(logs);
});
});