-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
69 lines (55 loc) · 1.82 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
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
module.exports = PlayerEngine
inherits(PlayerEngine, EventEmitter)
function PlayerEngine () {
if (!(this instanceof PlayerEngine)) {
return new PlayerEngine()
}
// Add an audio element as the actual engine
this.audio = document.createElement('audio')
// Emit audio element events from this class
var self = this
this.audio.addEventListener('timeupdate', function () {
self.emit('timeupdate', self.audio.currentTime)
})
this.audio.addEventListener('ended', function () {
self.emit('ended')
})
}
// Load a track to play into the engine
PlayerEngine.prototype.load = function (url) {
this.audio.src = url
}
// Start the audio playback
PlayerEngine.prototype.play = function () {
this.audio.play()
}
// Stop the audio playback, an resets to the beginning of the track
PlayerEngine.prototype.stop = function () {
this.pause()
this.audio.currentTime = 0
}
// Pauses the audio playback, can be resumed from the current state
PlayerEngine.prototype.pause = function () {
this.audio.pause()
}
// Return the URL of the current track when the player is active else return "false"
PlayerEngine.prototype.playing = function () {
return this.audio.paused === true ? false : this.audio.currentSrc
}
// Toggles the state of the player to playing/pause corresponding to the current state
PlayerEngine.prototype.toggle = function () {
this.audio.paused === true ? this.play() : this.pause()
}
// Sets volume to the given value, if no value is given give back the current volumne
PlayerEngine.prototype.volume = function (volume) {
if (volume !== undefined) {
this.audio.volume = volume
}
return this.audio.volume
}
// Sets the playback position
PlayerEngine.prototype.seek = function (time) {
this.audio.currentTime = time
}