-
Notifications
You must be signed in to change notification settings - Fork 6
/
play_audio.js
88 lines (78 loc) · 2.06 KB
/
play_audio.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
/*
* Daddelkiste Duomatic Version 0.94
* Javascript implementation of an "Advanced Slot Machine"
*
* Copyright 2017 Rainer Wess, Osnabrück, Germany
* Open Source / Freeware - released under GPL 2.0
*/
// create new audio object and load the audio file
var audioSprite = new Audio();
audioSprite.src = "sound/audio_sprite.mp3";
audioSprite.load();
audioSprite.pause();
// define the sprites
var spriteData = {
stille: {
start: 0.0,
length: 1.9
},
walzenstop: {
start: 2.0,
length: 4.9
},
abbuchen: {
start: 5.0,
length: 7.9
},
risiko1: {
start: 8.0,
length: 9.9
},
risiko2: {
start: 10.0,
length: 11.9
},
angenommen: {
start: 12.0,
length: 14.9
},
ausspielung: {
start: 15.0,
length: 29.9
},
hauptgewinn: {
start: 30.0,
length: 49.5
}
};
// current sprite being played
var currentSprite = {};
// time update handler to ensure we stop when a sprite is complete
var onTimeUpdate = function() {
if (this.currentTime >= currentSprite.start + currentSprite.length) {
this.pause();
}
};
audioSprite.addEventListener('timeupdate', onTimeUpdate, false);
// in mobile Safari, the first time this is called will load the audio. Ideally, we'd load the audio file completely before doing this.
var audio_play = function(id) {
if (spriteData[id] && spriteData[id].length) {
audioSprite.pause();
currentSprite = spriteData[id];
audioSprite.currentTime = currentSprite.start;
var playPromise = audioSprite.play();
if (playPromise !== undefined) {
playPromise.then(_ => {
// Automatic playback started!
})
.catch(error => {
// Auto-play was prevented
});
}
}
};
// sometimes, we want it just quiet and don't care which sprite is playing
var audio_stop = function() {
audioSprite.pause();
};
// ENDE