/
player-dom.ts
179 lines (154 loc) · 6.34 KB
/
player-dom.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category component/audio
*/
import { clamp } from '../../core/math/utils';
import { AudioPlayer, IAudioInfo, PlayingState } from './player';
export class AudioPlayerDOM extends AudioPlayer {
protected _volume = 1;
protected _loop = false;
protected _oneShoting = false;
protected _audio: HTMLAudioElement;
protected _cbRegistered = false;
private _remove_cb: () => void;
private _post_play: () => void;
private _on_gesture: () => void;
private _post_gesture: () => void;
constructor (info: IAudioInfo) {
super(info);
this._audio = info.clip;
this._remove_cb = () => {
if (!this._cbRegistered) { return; }
cc.game.canvas.removeEventListener('touchend', this._on_gesture);
cc.game.canvas.removeEventListener('mouseup', this._on_gesture);
this._cbRegistered = false;
};
this._post_play = () => {
this._state = PlayingState.PLAYING;
this._eventTarget.emit('started');
this._remove_cb(); // should remove callbacks after any success play
};
this._post_gesture = () => {
if (this._interrupted) { this._post_play(); this._interrupted = false; }
else { this._audio!.pause(); this._audio!.currentTime = 0; }
};
this._on_gesture = () => {
if (!this._audio) { return; }
const promise = this._audio.play();
if (!promise) { // Chrome50/Firefox53 below
// delay eval here to yield uniform behavior with other platforms
this._state = PlayingState.PLAYING;
cc.director.once(cc.Director.EVENT_AFTER_UPDATE, this._post_gesture);
return;
}
promise.then(this._post_gesture);
this._remove_cb();
};
this._audio.volume = this._volume;
this._audio.loop = this._loop;
// callback on audio ended
this._audio.addEventListener('ended', () => {
if (this._oneShoting) { return; }
this._state = PlayingState.STOPPED;
this._audio!.currentTime = 0;
this._eventTarget.emit('ended');
});
/* play & stop immediately after receiving a gesture so that
we can freely invoke play() outside event listeners later */
cc.game.canvas.addEventListener('touchend', this._on_gesture);
cc.game.canvas.addEventListener('mouseup', this._on_gesture);
this._cbRegistered = true;
}
public play () {
if (!this._audio || this._state === PlayingState.PLAYING) { return; }
if (this._blocking) { this._interrupted = true; return; }
const promise = this._audio.play();
if (!promise) {
// delay eval here to yield uniform behavior with other platforms
this._state = PlayingState.PLAYING;
cc.director.once(cc.Director.EVENT_AFTER_UPDATE, this._post_play);
return;
}
promise.then(this._post_play).catch(() => { this._interrupted = true; });
}
public pause () {
if (!this._audio || this._state !== PlayingState.PLAYING) { return; }
this._audio.pause();
this._state = PlayingState.STOPPED;
this._oneShoting = false;
}
public stop () {
if (!this._audio) { return; }
this._audio.currentTime = 0;
if (this._state !== PlayingState.PLAYING) { return; }
this._audio.pause();
this._state = PlayingState.STOPPED;
this._oneShoting = false;
}
public playOneShot (volume = 1) {
/* HTMLMediaElement doesn't support multiple playback at the
same time so here we fall back to re-start style approach */
const clip = this._audio;
if (!clip) { return; }
clip.currentTime = 0;
clip.volume = volume;
if (this._oneShoting) { return; }
clip.loop = false;
this._oneShoting = true;
clip.play().then(() => {
clip.addEventListener('ended', () => {
clip.currentTime = 0;
clip.volume = this._volume;
clip.loop = this._loop;
this._oneShoting = false;
}, { once: true });
}).catch(() => { this._oneShoting = false; });
}
public setCurrentTime (val: number) {
if (!this._audio) { return; }
this._audio.currentTime = clamp(val, 0, this._duration);
}
public getCurrentTime () {
return this._audio ? this._audio.currentTime : 0;
}
public setVolume (val: number, immediate: boolean) {
this._volume = val;
/* note this won't work for ios devices, for there
is just no way to set HTMLMediaElement's volume */
if (this._audio) { this._audio.volume = val; }
}
public getVolume () {
if (this._audio) { return this._audio.volume; }
return this._volume;
}
public setLoop (val: boolean) {
this._loop = val;
if (this._audio) { this._audio.loop = val; }
}
public getLoop () {
return this._loop;
}
public destroy () {
if (this._audio) { this._audio.src = ''; }
super.destroy();
}
}