/
CCAudioSource.js
333 lines (300 loc) · 9.51 KB
/
CCAudioSource.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://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.
****************************************************************************/
const misc = require('../utils/misc');
const Component = require('./CCComponent');
const AudioClip = require('../assets/CCAudioClip');
/**
* !#en Audio Source.
* !#zh 音频源组件,能对音频剪辑。
* @class AudioSource
* @extends Component
*/
var AudioSource = cc.Class({
name: 'cc.AudioSource',
extends: Component,
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.others/AudioSource',
help: 'i18n:COMPONENT.help_url.audiosource',
},
ctor: function () {
// We can't require Audio here because jsb Audio is implemented outside the engine,
// it can only take effect rely on overwriting cc._Audio
this.audio = new cc._Audio();
},
properties: {
_clip: {
default: null,
type: AudioClip
},
_volume: 1,
_mute: false,
_loop: false,
_pausedFlag: {
default: false,
serializable: false
},
_firstlyEnabled: true,
/**
* !#en
* Is the audio source playing (Read Only). <br/>
* Note: isPlaying is not supported for Native platforms.
* !#zh
* 该音频剪辑是否正播放(只读)。<br/>
* 注意:Native 平台暂时不支持 isPlaying。
* @property isPlaying
* @type {Boolean}
* @readOnly
* @default false
*/
isPlaying: {
get: function () {
var state = this.audio.getState();
return state === cc._Audio.State.PLAYING;
},
visible: false
},
/**
* !#en The clip of the audio source to play.
* !#zh 要播放的音频剪辑。
* @property clip
* @type {AudioClip}
* @default 1
*/
clip: {
get: function () {
return this._clip;
},
set: function (value) {
if (value === this._clip) {
return;
}
if (!(value instanceof AudioClip)) {
return cc.error('Wrong type of AudioClip.');
}
this._clip = value;
this.audio.stop();
this.audio.src = this._clip;
if (this.preload) {
this._clip._ensureLoaded();
}
},
type: AudioClip,
tooltip: CC_DEV && 'i18n:COMPONENT.audio.clip',
animatable: false
},
/**
* !#en The volume of the audio source.
* !#zh 音频源的音量(0.0 ~ 1.0)。
* @property volume
* @type {Number}
* @default 1
*/
volume: {
get: function () {
return this._volume;
},
set: function (value) {
value = misc.clamp01(value);
this._volume = value;
if (!this._mute) {
this.audio.setVolume(value);
}
return value;
},
tooltip: CC_DEV && 'i18n:COMPONENT.audio.volume'
},
/**
* !#en Is the audio source mute?
* !#zh 是否静音音频源。Mute 是设置音量为 0,取消静音是恢复原来的音量。
* @property mute
* @type {Boolean}
* @default false
*/
mute: {
get: function () {
return this._mute;
},
set: function (value) {
this._mute = value;
this.audio.setVolume(value ? 0 : this._volume);
return value;
},
animatable: false,
tooltip: CC_DEV && 'i18n:COMPONENT.audio.mute',
},
/**
* !#en Is the audio source looping?
* !#zh 音频源是否循环播放?
* @property loop
* @type {Boolean}
* @default false
*/
loop: {
get: function () {
return this._loop;
},
set: function (value) {
this._loop = value;
this.audio.setLoop(value);
return value;
},
animatable: false,
tooltip: CC_DEV && 'i18n:COMPONENT.audio.loop'
},
/**
* !#en If set to true, the audio source will automatically start playing on onEnable.
* !#zh 如果设置为 true,音频源将在 onEnable 时自动播放。
* @property playOnLoad
* @type {Boolean}
* @default true
*/
playOnLoad: {
default: false,
tooltip: CC_DEV && 'i18n:COMPONENT.audio.play_on_load',
animatable: false
},
/**
* !#en If set to true and AudioClip is a deferred load resource, the component will preload AudioClip in the onLoad phase.
* !#zh 如果设置为 true 且 AudioClip 为延迟加载资源,组件将在 onLoad 阶段预加载 AudioClip。
* @property preload
* @type {Boolean}
* @default false
*/
preload: {
default: false,
animatable: false
}
},
_pausedCallback: function () {
var state = this.audio.getState();
if (state === cc._Audio.State.PLAYING) {
this.audio.pause();
this._pausedFlag = true;
}
},
_restoreCallback: function () {
if (this._pausedFlag) {
this.audio.resume();
}
this._pausedFlag = false;
},
onLoad: function () {
// this.audio.src is undefined, when the clip property is deserialized from the scene
if (!this.audio.src) {
this.audio.src = this._clip;
}
if (this.preload) {
this._clip._ensureLoaded();
}
},
onEnable: function () {
if (this.playOnLoad && this._firstlyEnabled) {
this._firstlyEnabled = false;
this.play();
}
cc.game.on(cc.game.EVENT_HIDE, this._pausedCallback, this);
cc.game.on(cc.game.EVENT_SHOW, this._restoreCallback, this);
},
onDisable: function () {
this.stop();
cc.game.off(cc.game.EVENT_HIDE, this._pausedCallback, this);
cc.game.off(cc.game.EVENT_SHOW, this._restoreCallback, this);
},
onDestroy: function () {
this.audio.destroy();
},
/**
* !#en Plays the clip.
* !#zh 播放音频剪辑。
* @method play
*/
play: function () {
if ( CC_EDITOR || !this._clip ) return;
var audio = this.audio;
audio.setVolume(this._mute ? 0 : this._volume);
audio.setLoop(this._loop);
audio.setCurrentTime(0);
audio.play();
},
/**
* !#en Stops the clip.
* !#zh 停止当前音频剪辑。
* @method stop
*/
stop: function () {
this.audio.stop();
},
/**
* !#en Pause the clip.
* !#zh 暂停当前音频剪辑。
* @method pause
*/
pause: function () {
this.audio.pause();
},
/**
* !#en Resume the clip.
* !#zh 恢复播放。
* @method resume
*/
resume: function () {
this.audio.resume();
},
/**
* !#en Rewind playing music.
* !#zh 从头开始播放。
* @method rewind
*/
rewind: function(){
this.audio.setCurrentTime(0);
},
/**
* !#en Get current time
* !#zh 获取当前的播放时间
* @method getCurrentTime
* @return {Number}
*/
getCurrentTime: function () {
return this.audio.getCurrentTime();
},
/**
* !#en Set current time
* !#zh 设置当前的播放时间
* @method setCurrentTime
* @param {Number} time
* @return {Number}
*/
setCurrentTime: function (time) {
this.audio.setCurrentTime(time);
return time;
},
/**
* !#en Get audio duration
* !#zh 获取当前音频的长度
* @method getDuration
* @return {Number}
*/
getDuration: function () {
return this.audio.getDuration();
}
});
cc.AudioSource = module.exports = AudioSource;