mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
audio system (#3937)
* audio adapt * compile shaders as early as possible
This commit is contained in:
committed by
Weiliang Wang
parent
5e17024da2
commit
1b2edaec93
@@ -0,0 +1,100 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import { Asset } from '../../../assets/asset';
|
||||
import { ccclass, property } from '../../../core/data/class-decorator';
|
||||
import { EventTargetFactory } from '../../../core/event/event-target-factory';
|
||||
import { AudioPlayer, PlayingState } from './player';
|
||||
import { AudioPlayerDOM } from './player-dom';
|
||||
import { AudioPlayerWeb } from './player-web';
|
||||
|
||||
export const AudioType = {
|
||||
UNKNOWN_AUDIO: -1,
|
||||
WEB_AUDIO: 0,
|
||||
DOM_AUDIO: 1,
|
||||
WX_GAME_AUDIO: 2,
|
||||
};
|
||||
|
||||
@ccclass('cc.AudioClip')
|
||||
export class AudioClip extends EventTargetFactory(Asset) {
|
||||
|
||||
public set _nativeAsset (clip: any) {
|
||||
this._audio = clip;
|
||||
if (clip) {
|
||||
let ctor: any;
|
||||
if (clip instanceof AudioBuffer) {
|
||||
ctor = AudioPlayerWeb;
|
||||
this._loadMode = AudioType.WEB_AUDIO;
|
||||
} else {
|
||||
ctor = AudioPlayerDOM;
|
||||
this._loadMode = AudioType.DOM_AUDIO;
|
||||
}
|
||||
this._player = new ctor({ clip, duration: this._duration, eventTarget: this });
|
||||
this.loaded = true;
|
||||
this.emit('load');
|
||||
} else {
|
||||
this._player = null;
|
||||
this._loadMode = AudioType.UNKNOWN_AUDIO;
|
||||
this._duration = 0;
|
||||
this.loaded = false;
|
||||
}
|
||||
}
|
||||
|
||||
public get _nativeAsset () {
|
||||
return this._audio;
|
||||
}
|
||||
|
||||
get loadMode () {
|
||||
return this._loadMode;
|
||||
}
|
||||
|
||||
get state () {
|
||||
return this._player ? this._player.getState() : PlayingState.INITIALIZING;
|
||||
}
|
||||
|
||||
public static PlayingState = PlayingState;
|
||||
public static AudioType = AudioType;
|
||||
@property // we serialize this because it's unavailable at runtime on some platforms
|
||||
protected _duration = 0;
|
||||
@property(AudioType)
|
||||
protected _loadMode = AudioType.UNKNOWN_AUDIO;
|
||||
|
||||
private _audio: any = null;
|
||||
private _player: AudioPlayer | null = null;
|
||||
|
||||
public play () { if (this._player) { this._player.play(); } }
|
||||
public pause () { if (this._player) { this._player.pause(); } }
|
||||
public stop () { if (this._player) { this._player.stop(); } }
|
||||
public playOneShot (volume: number) { if (this._player) { this._player.playOneShot(volume); } }
|
||||
public setCurrentTime (val: number) { if (this._player) { this._player.setCurrentTime(val); } }
|
||||
public getCurrentTime () { if (this._player) { return this._player.getCurrentTime(); } return 0; }
|
||||
public getDuration () { if (this._player) { return this._player.getDuration(); } return this._duration; }
|
||||
public setVolume (val: number) { if (this._player) { this._player.setVolume(val); } }
|
||||
public getVolume () { if (this._player) { return this._player.getVolume(); } return 1; }
|
||||
public setLoop (val: boolean) { if (this._player) { this._player.setLoop(val); } }
|
||||
public getLoop () { if (this._player) { return this._player.getLoop(); } return false; }
|
||||
}
|
||||
|
||||
cc.AudioClip = AudioClip;
|
||||
@@ -23,29 +23,26 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { ccclass } from '../../core/data/class-decorator';
|
||||
import { AudioClip, AudioSourceType, IAudioInfo, PlayingState } from './audio-clip';
|
||||
import { AudioPlayer, IAudioInfo, PlayingState } from './player';
|
||||
|
||||
@ccclass('cc.DOMAudioClip')
|
||||
export class DOMAudioClip extends AudioClip {
|
||||
export class AudioPlayerDOM extends AudioPlayer {
|
||||
protected _volume = 1;
|
||||
protected _loop = false;
|
||||
protected _currentTimer = 0;
|
||||
protected _oneShoting = false;
|
||||
protected _audio: HTMLAudioElement | null = null;
|
||||
protected _audio: HTMLAudioElement;
|
||||
|
||||
private _post_play: () => void;
|
||||
private _on_gesture: () => void;
|
||||
private _alreadyDelayed = false;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
this._loadMode = AudioSourceType.DOM_AUDIO;
|
||||
constructor (info: IAudioInfo) {
|
||||
super(info);
|
||||
this._audio = info.clip;
|
||||
|
||||
this._post_play = () => {
|
||||
this._state = PlayingState.PLAYING;
|
||||
this.emit('started');
|
||||
this._eventTarget.emit('started');
|
||||
};
|
||||
|
||||
this._on_gesture = () => {
|
||||
@@ -62,19 +59,15 @@ export class DOMAudioClip extends AudioClip {
|
||||
document.removeEventListener('mouseup', this._on_gesture);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public setNativeAsset (clip: HTMLAudioElement) {
|
||||
super._nativeAsset = clip;
|
||||
clip.volume = this._volume;
|
||||
clip.loop = this._loop;
|
||||
this._audio.volume = this._volume;
|
||||
this._audio.loop = this._loop;
|
||||
// callback on audio ended
|
||||
clip.addEventListener('ended', () => {
|
||||
this._audio.addEventListener('ended', () => {
|
||||
if (this._oneShoting) { return; }
|
||||
this._state = PlayingState.STOPPED;
|
||||
clip.currentTime = 0;
|
||||
// @ts-ignore
|
||||
this.emit('ended');
|
||||
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 */
|
||||
@@ -164,5 +157,3 @@ export class DOMAudioClip extends AudioClip {
|
||||
return this._loop;
|
||||
}
|
||||
}
|
||||
|
||||
cc.DOMAudioClip = DOMAudioClip;
|
||||
@@ -23,11 +23,12 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { ccclass } from '../../core/data/class-decorator';
|
||||
import { AudioClip, AudioSourceType, IAudioInfo, PlayingState } from './audio-clip';
|
||||
import sys from '../../../core/platform/CCSys';
|
||||
import { AudioPlayer, IAudioInfo, PlayingState } from './player';
|
||||
|
||||
@ccclass('cc.WebAudioClip')
|
||||
export default class WebAudioClip extends AudioClip {
|
||||
const audioSupport = sys.__audioSupport;
|
||||
|
||||
export class AudioPlayerWeb extends AudioPlayer {
|
||||
protected _startTime = 0;
|
||||
protected _offset = 0;
|
||||
protected _volume = 1;
|
||||
@@ -43,12 +44,11 @@ export default class WebAudioClip extends AudioClip {
|
||||
private _on_gesture: () => void;
|
||||
private _alreadyDelayed = false;
|
||||
|
||||
constructor (context: AudioContext) {
|
||||
super();
|
||||
constructor (info: IAudioInfo) {
|
||||
super(info);
|
||||
this._audio = info.clip;
|
||||
|
||||
this._loadMode = AudioSourceType.WEB_AUDIO;
|
||||
|
||||
this._context = context;
|
||||
this._context = audioSupport.context;
|
||||
this._sourceNode = this._context.createBufferSource();
|
||||
this._gainNode = this._context.createGain();
|
||||
this._gainNode.connect(this._context.destination);
|
||||
@@ -57,8 +57,7 @@ export default class WebAudioClip extends AudioClip {
|
||||
this._offset = 0;
|
||||
this._startTime = this._context.currentTime;
|
||||
if (this._sourceNode.loop) { return; }
|
||||
// @ts-ignore
|
||||
this.emit('ended');
|
||||
this._eventTarget.emit('ended');
|
||||
this._state = PlayingState.STOPPED;
|
||||
};
|
||||
|
||||
@@ -71,7 +70,7 @@ export default class WebAudioClip extends AudioClip {
|
||||
this._state = PlayingState.PLAYING;
|
||||
this._startTime = this._context.currentTime;
|
||||
// delay eval here to yield uniform behavior with other platforms
|
||||
cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { this.emit('started'); });
|
||||
cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { this._eventTarget.emit('started'); });
|
||||
/* still not supported by all platforms *
|
||||
this._sourceNode.onended = this._on_ended;
|
||||
/* doing it manually for now */
|
||||
@@ -92,10 +91,7 @@ export default class WebAudioClip extends AudioClip {
|
||||
document.removeEventListener('mouseup', this._on_gesture);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
public set _nativeAsset (clip: AudioBuffer) {
|
||||
super._nativeAsset = clip;
|
||||
if (this._context.state === 'running') { return; }
|
||||
window.addEventListener('touchend', this._on_gesture);
|
||||
document.addEventListener('mouseup', this._on_gesture);
|
||||
@@ -173,5 +169,3 @@ export default class WebAudioClip extends AudioClip {
|
||||
return this._loop;
|
||||
}
|
||||
}
|
||||
|
||||
cc.WebAudioClip = WebAudioClip;
|
||||
@@ -25,7 +25,7 @@
|
||||
// @ts-check
|
||||
import { _decorator } from "../../core/data/index";
|
||||
const { ccclass } = _decorator;
|
||||
import { AudioClip, AudioSourceType, PlayingState } from './audio-clip';
|
||||
import { AudioClip, AudioType, PlayingState } from './audio-clip';
|
||||
|
||||
/**
|
||||
* WeChat audio to port. https://developers.weixin.qq.com/minigame/dev/document/media/audio/InnerAudioContext.html
|
||||
@@ -34,7 +34,7 @@ import { AudioClip, AudioSourceType, PlayingState } from './audio-clip';
|
||||
export default class WxGameAudioClip extends AudioClip {
|
||||
constructor() {
|
||||
super();
|
||||
this.loadMode = AudioSourceType.WX_GAME_AUDIO;
|
||||
this.loadMode = AudioType.WX_GAME_AUDIO;
|
||||
this._volume = 1;
|
||||
this._loop = false;
|
||||
this._oneShoting = false;
|
||||
@@ -153,4 +153,4 @@ export default class WxGameAudioClip extends AudioClip {
|
||||
}
|
||||
}
|
||||
|
||||
cc.WxGameAudioClip = WxGameAudioClip;
|
||||
cc.WxGameAudioClip = WxGameAudioClip;
|
||||
@@ -23,64 +23,38 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { Asset } from '../../assets/asset';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { EventTargetFactory } from '../../core/event/event-target-factory';
|
||||
|
||||
export const PlayingState = {
|
||||
INITIALIZING: 0,
|
||||
PLAYING: 1,
|
||||
STOPPED: 2,
|
||||
};
|
||||
|
||||
export const AudioSourceType = {
|
||||
UNKNOWN_AUDIO: -1,
|
||||
/**
|
||||
* load through Web Audio API interface
|
||||
*/
|
||||
WEB_AUDIO: 0,
|
||||
/**
|
||||
* load through an audio DOM element
|
||||
*/
|
||||
DOM_AUDIO: 1,
|
||||
|
||||
WX_GAME_AUDIO: 2,
|
||||
};
|
||||
|
||||
@ccclass('cc.AudioClip')
|
||||
export class AudioClip extends EventTargetFactory(Asset) {
|
||||
protected _audio: any = null;
|
||||
protected _state = PlayingState.INITIALIZING;
|
||||
|
||||
@property // we serialize this because it's unavailable at runtime on some platforms
|
||||
protected _duration = 0;
|
||||
|
||||
@property(AudioSourceType)
|
||||
protected _loadMode = AudioSourceType.UNKNOWN_AUDIO;
|
||||
|
||||
public set _nativeAsset (clip: any) {
|
||||
this._audio = clip;
|
||||
if (clip) {
|
||||
this.loaded = true;
|
||||
this.emit('load');
|
||||
this._state = PlayingState.STOPPED;
|
||||
} else {
|
||||
this.loaded = false;
|
||||
this._state = PlayingState.INITIALIZING;
|
||||
this._duration = 0;
|
||||
}
|
||||
}
|
||||
public get _nativeAsset () {
|
||||
return this._audio;
|
||||
}
|
||||
|
||||
get loadMode () {
|
||||
return this._loadMode;
|
||||
}
|
||||
|
||||
get state () {
|
||||
return this._state;
|
||||
}
|
||||
export interface IAudioInfo {
|
||||
clip: any;
|
||||
duration: number;
|
||||
eventTarget: any;
|
||||
}
|
||||
|
||||
cc.AudioClip = AudioClip;
|
||||
export abstract class AudioPlayer {
|
||||
protected _state = PlayingState.STOPPED;
|
||||
protected _duration = 0;
|
||||
protected _eventTarget: any;
|
||||
|
||||
constructor (info: IAudioInfo) {
|
||||
this._duration = info.duration;
|
||||
this._eventTarget = info.eventTarget;
|
||||
}
|
||||
|
||||
public abstract play (): void;
|
||||
public abstract pause (): void;
|
||||
public abstract stop (): void;
|
||||
public abstract playOneShot (volume: number): void;
|
||||
public abstract setCurrentTime (val: number): void;
|
||||
public abstract getCurrentTime (): number;
|
||||
public abstract getDuration (): number;
|
||||
public abstract setVolume (val: number): void;
|
||||
public abstract getVolume (): number;
|
||||
public abstract setLoop (val: boolean): void;
|
||||
public abstract getLoop (): boolean;
|
||||
public getState () { return this._state; }
|
||||
}
|
||||
@@ -24,8 +24,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
export { default as AnimationClip } from "./animation-clip";
|
||||
export { AudioClip } from "./audio-clip";
|
||||
export { DOMAudioClip } from "./dom-audio-clip";
|
||||
export { AudioClip } from "./audio/clip";
|
||||
export { BitmapFont } from "./bitmap-font";
|
||||
export { EffectAsset } from "./effect-asset";
|
||||
export { Material } from "./material";
|
||||
@@ -33,7 +32,5 @@ export { Mesh } from "./mesh";
|
||||
export { default as OpentypeFont } from "./opentype-font";
|
||||
export { default as Skeleton } from "./skeleton";
|
||||
export { default as Sprite } from "./sprite";
|
||||
export { default as WebAudioClip } from "./web-audio-clip";
|
||||
export { default as WxGameAudioClip } from "./wx-game-audio-clip";
|
||||
|
||||
export { PhysicsMaterial } from './physics/material';
|
||||
|
||||
@@ -267,7 +267,7 @@ class AnimationCtrl {
|
||||
@executionOrder(99)
|
||||
@executeInEditMode
|
||||
@menu('Components/AnimationComponent')
|
||||
export default class AnimationComponent extends Component {
|
||||
export class AnimationComponent extends Component {
|
||||
@property({ type: [AnimationClip] })
|
||||
public _clips: AnimationClip[] = [];
|
||||
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/****************************************************************************
|
||||
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.
|
||||
****************************************************************************/
|
||||
import { Component } from '../../components/component';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { AudioClip } from '../assets/audio/clip';
|
||||
|
||||
/**
|
||||
* A representation of a single audio source,
|
||||
* contains basic functionalities like play, pause and stop.
|
||||
*/
|
||||
@ccclass('cc.AudioSourceComponent')
|
||||
export class AudioSourceComponent extends Component {
|
||||
@property(AudioClip)
|
||||
protected _clip: AudioClip | null = null;
|
||||
@property
|
||||
protected _loop = false;
|
||||
@property
|
||||
protected _playOnAwake = true;
|
||||
@property
|
||||
protected _volume = 1;
|
||||
|
||||
private _cachedCurrentTime = 0;
|
||||
|
||||
/**
|
||||
* The default AudioClip to play
|
||||
*/
|
||||
@property({ type: AudioClip })
|
||||
set clip (val) {
|
||||
this._clip = val;
|
||||
this._syncStates();
|
||||
}
|
||||
get clip () {
|
||||
return this._clip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the audio clip looping?
|
||||
*/
|
||||
@property({ type: Boolean })
|
||||
set loop (val) {
|
||||
this._loop = val;
|
||||
if (this._clip) { this._clip.setLoop(val); }
|
||||
}
|
||||
get loop () {
|
||||
return this._loop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the autoplay enabled?
|
||||
* Note that for the most time now the autoplay will only starts
|
||||
* after a user gesture is received, according to the latest autoplay policy:
|
||||
* https://www.chromium.org/audio-video/autoplay
|
||||
*/
|
||||
@property({ type: Boolean })
|
||||
set playOnAwake (val) {
|
||||
this._playOnAwake = val;
|
||||
}
|
||||
get playOnAwake () {
|
||||
return this._playOnAwake;
|
||||
}
|
||||
|
||||
/**
|
||||
* The volume of this audio source (0.0 to 1.0).
|
||||
*/
|
||||
@property({ type: Number })
|
||||
set volume (val) {
|
||||
if (this._clip) {
|
||||
this._clip.setVolume(val);
|
||||
// on some platform volume control may not be available
|
||||
this._volume = this._clip.getVolume();
|
||||
} else {
|
||||
this._volume = val;
|
||||
}
|
||||
}
|
||||
get volume () {
|
||||
return this._volume;
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this._syncStates();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the clip
|
||||
*/
|
||||
public play () {
|
||||
if (!this._clip) { return; }
|
||||
if (this.playing) { this.currentTime = 0; }
|
||||
else { this._clip.play(); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause the clip
|
||||
*/
|
||||
public pause () {
|
||||
if (!this._clip) { return; }
|
||||
this._clip.pause();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the clip
|
||||
*/
|
||||
public stop () {
|
||||
if (!this._clip) { return; }
|
||||
this._clip.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays an AudioClip, and scales volume by volumeScale.
|
||||
* @param clip - the clip being played
|
||||
* @param volumeScale - the scale of the volume (0-1).
|
||||
*/
|
||||
public playOneShot (clip: AudioClip, volumeScale = 1) {
|
||||
clip.playOneShot(this._volume * volumeScale);
|
||||
}
|
||||
|
||||
protected _syncStates () {
|
||||
if (!this._clip) { return; }
|
||||
this._clip.setCurrentTime(this._cachedCurrentTime);
|
||||
this._clip.setLoop(this._loop);
|
||||
this.volume = this._volume;
|
||||
if (this._playOnAwake) { this.play(); }
|
||||
}
|
||||
|
||||
/**
|
||||
* set current playback time, in seconds
|
||||
* @param num the playback time you want to jump to
|
||||
*/
|
||||
set currentTime (num: number) {
|
||||
this._cachedCurrentTime = num;
|
||||
if (!this._clip) { return; }
|
||||
this._clip.setCurrentTime(this._cachedCurrentTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current playback time, in seconds
|
||||
* @returns time current playback time
|
||||
*/
|
||||
get currentTime () {
|
||||
if (!this._clip) { return this._cachedCurrentTime; }
|
||||
return this._clip.getCurrentTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the audio duration, in seconds
|
||||
* @returns audio duration
|
||||
*/
|
||||
get duration () {
|
||||
if (!this._clip) { return 0; }
|
||||
return this._clip.getDuration();
|
||||
}
|
||||
|
||||
/**
|
||||
* get current audio state
|
||||
* @returns current audio state
|
||||
*/
|
||||
get state () {
|
||||
if (!this._clip) { return AudioClip.PlayingState.INITIALIZING; }
|
||||
return this._clip.state;
|
||||
}
|
||||
|
||||
/**
|
||||
* is the audio currently playing?
|
||||
*/
|
||||
get playing () {
|
||||
return this.state === AudioClip.PlayingState.PLAYING;
|
||||
}
|
||||
}
|
||||
+15
-11
@@ -24,38 +24,42 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import AnimationComponent from './animation-component';
|
||||
import { AnimationComponent } from './animation-component';
|
||||
import { AudioSourceComponent } from './audio-source-component';
|
||||
import { CameraComponent } from './camera-component';
|
||||
import LightComponent from './light-component';
|
||||
import { LightComponent } from './light-component';
|
||||
import { ModelComponent } from './model-component';
|
||||
import { ParticleSystemComponent } from './particle/particle-system-component';
|
||||
import { BoxColliderComponent, SphereColliderComponent } from './physics/collider-component';
|
||||
import { RigidBodyComponent } from './physics/rigid-body-component';
|
||||
import SkinningModelComponent from './skinning-model-component';
|
||||
import SkyboxComponent from './skybox-component';
|
||||
import { SkinningModelComponent } from './skinning-model-component';
|
||||
import { SkyboxComponent } from './skybox-component';
|
||||
export * from './physics';
|
||||
import ParticleSystemComponent from './particle/particle-system-component';
|
||||
|
||||
export {
|
||||
CameraComponent,
|
||||
AnimationComponent,
|
||||
AudioSourceComponent,
|
||||
CameraComponent,
|
||||
LightComponent,
|
||||
ModelComponent,
|
||||
SkinningModelComponent,
|
||||
SkyboxComponent,
|
||||
|
||||
BoxColliderComponent,
|
||||
SphereColliderComponent,
|
||||
RigidBodyComponent,
|
||||
ParticleSystemComponent,
|
||||
RigidBodyComponent,
|
||||
SphereColliderComponent,
|
||||
};
|
||||
|
||||
cc.CameraComponent = CameraComponent;
|
||||
cc.AnimationComponent = AnimationComponent;
|
||||
cc.AudioSourceComponent = AudioSourceComponent;
|
||||
cc.CameraComponent = CameraComponent;
|
||||
cc.LightComponent = LightComponent;
|
||||
cc.ModelComponent = ModelComponent;
|
||||
cc.SkinningModelComponent = SkinningModelComponent;
|
||||
cc.SkyboxComponent = SkyboxComponent;
|
||||
|
||||
cc.RigidBodyComponent = RigidBodyComponent;
|
||||
cc.BoxColliderComponent = BoxColliderComponent;
|
||||
cc.SphereColliderComponent = SphereColliderComponent;
|
||||
cc.ParticleSystemComponent = ParticleSystemComponent;
|
||||
cc.RigidBodyComponent = RigidBodyComponent;
|
||||
cc.SphereColliderComponent = SphereColliderComponent;
|
||||
|
||||
@@ -115,7 +115,7 @@ const LightShadowType = Enum({
|
||||
@ccclass('cc.LightComponent')
|
||||
@menu('Components/LightComponent')
|
||||
@executeInEditMode
|
||||
export default class LightComponent extends Component {
|
||||
export class LightComponent extends Component {
|
||||
|
||||
public static Type = Type;
|
||||
public static ShadowType = LightShadowType;
|
||||
|
||||
@@ -27,7 +27,7 @@ const _world_mat = mat4.create();
|
||||
@requireComponent(ParticleSystemRenderer)
|
||||
@executionOrder(99)
|
||||
@executeInEditMode
|
||||
export default class ParticleSystemComponent extends Component {
|
||||
export class ParticleSystemComponent extends Component {
|
||||
|
||||
@property
|
||||
public capacity = 2000;
|
||||
|
||||
@@ -45,7 +45,7 @@ type SkinningTarget = Map<string, Node>;
|
||||
@executionOrder(100)
|
||||
@executeInEditMode
|
||||
@menu('Components/SkinningModelComponent')
|
||||
export default class SkinningModelComponent extends ModelComponent {
|
||||
export class SkinningModelComponent extends ModelComponent {
|
||||
|
||||
/**
|
||||
* !#en The bone nodes
|
||||
|
||||
@@ -44,7 +44,7 @@ import { box } from '../primitive';
|
||||
@ccclass('cc.SkyboxComponent')
|
||||
@menu('Components/SkyboxComponent')
|
||||
@executeInEditMode
|
||||
export default class SkyboxComponent extends Component {
|
||||
export class SkyboxComponent extends Component {
|
||||
|
||||
@property({
|
||||
type: TextureCube,
|
||||
|
||||
@@ -27,10 +27,9 @@
|
||||
import { CameraComponent } from '../../../3d/framework/camera-component';
|
||||
import { Component } from '../../../components/component';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../core/data/class-decorator';
|
||||
import { Color, Rect, Size, Vec3 } from '../../../core/value-types';
|
||||
import { Size, Vec3 } from '../../../core/value-types';
|
||||
import { GFXClearFlag } from '../../../gfx/define';
|
||||
import { Camera } from '../../../renderer/scene/camera';
|
||||
import { Node } from '../../../scene-graph/node';
|
||||
|
||||
const _tempPos = new Vec3();
|
||||
const _worldPos = new Vec3();
|
||||
@@ -169,7 +168,7 @@ export class CanvasComponent extends Component {
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
const cameraNode = new Node('UICamera');
|
||||
const cameraNode = new cc.Node('UICamera');
|
||||
cameraNode.setPosition(0, 0, 1000);
|
||||
if (!CC_EDITOR) {
|
||||
this._camera = cc.director.root.ui.renderScene.createCamera({
|
||||
@@ -189,7 +188,6 @@ export class CanvasComponent extends Component {
|
||||
this._camera!.resize(device.width, device.height);
|
||||
}
|
||||
|
||||
|
||||
cc.director.root.ui.addScreen(this);
|
||||
|
||||
// if (CC_DEV) {
|
||||
|
||||
@@ -3,8 +3,6 @@ import { ccclass, executeInEditMode, executionOrder, menu, property } from '../.
|
||||
import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/value-types';
|
||||
import * as math from '../../../core/vmath/index';
|
||||
import { EventType } from '../../../scene-graph/node-event-enum';
|
||||
import { MaskComponent } from './mask-component';
|
||||
import { UIRenderComponent } from './ui-render-component';
|
||||
|
||||
const _vec2a = new Vec2();
|
||||
const _vec2b = new Vec2();
|
||||
@@ -201,7 +199,7 @@ export class UITransformComponent extends Component {
|
||||
const cameraPt = _vec2a;
|
||||
const testPt = _vec2b;
|
||||
|
||||
const renderComp = this.node.getComponent(UIRenderComponent);
|
||||
const renderComp = this.node.getComponent(cc.UIRenderComponent) as any;
|
||||
if (!renderComp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { widgetManager } from './widget-manager';
|
||||
import { Component} from '../../../components/component';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property, requireComponent } from '../../../core/data/class-decorator';
|
||||
import { Node } from '../../../scene-graph/index';
|
||||
@@ -115,7 +114,7 @@ export class WidgetComponent extends Component {
|
||||
this._target = value;
|
||||
if (CC_EDITOR /*&& !cc.engine._isPlaying*/ && this.node.parent) {
|
||||
// adjust the offsets to keep the size and position unchanged after target chagned
|
||||
widgetManager.updateOffsetsToStayPut(this);
|
||||
cc._widgetManager.updateOffsetsToStayPut(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,7 +561,7 @@ export class WidgetComponent extends Component {
|
||||
* cc.log(widget.node.y); // changed
|
||||
*/
|
||||
public updateAlignment () {
|
||||
widgetManager.updateAlignment(this.node);
|
||||
cc._widgetManager.updateAlignment(this.node);
|
||||
}
|
||||
|
||||
protected onLoad () {
|
||||
@@ -579,11 +578,11 @@ export class WidgetComponent extends Component {
|
||||
}
|
||||
|
||||
protected onEnable () {
|
||||
widgetManager.add(this);
|
||||
cc._widgetManager.add(this);
|
||||
}
|
||||
|
||||
protected onDisable () {
|
||||
widgetManager.remove(this);
|
||||
cc._widgetManager.remove(this);
|
||||
}
|
||||
|
||||
private _setAlign (flag: AlignFlags, isAlign: boolean) {
|
||||
@@ -619,7 +618,7 @@ export class WidgetComponent extends Component {
|
||||
|
||||
if (CC_EDITOR && !cc.engine._isPlaying && this.node.parent) {
|
||||
// adjust the offsets to keep the size and position unchanged after alignment chagned
|
||||
widgetManager.updateOffsetsToStayPut(this, flag);
|
||||
cc._widgetManager.updateOffsetsToStayPut(this, flag);
|
||||
}
|
||||
} else {
|
||||
if (isHorizontal) {
|
||||
|
||||
@@ -1,505 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2008-2010 Ricardo Quesada
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 EventTarget = require('../core/event/event-target');
|
||||
const sys = require('../core/platform/CCSys');
|
||||
const LoadMode = require('../core/assets/CCAudioClip').LoadMode;
|
||||
|
||||
let touchBinded = false;
|
||||
let touchPlayList = [
|
||||
//{ instance: Audio, offset: 0, audio: audio }
|
||||
];
|
||||
|
||||
let Audio = function (src) {
|
||||
EventTarget.call(this);
|
||||
|
||||
this._src = src;
|
||||
this._element = null;
|
||||
this.id = 0;
|
||||
|
||||
this._volume = 1;
|
||||
this._loop = false;
|
||||
this._nextTime = 0; // playback position to set
|
||||
|
||||
this._state = Audio.State.INITIALZING;
|
||||
|
||||
this._onended = function () {
|
||||
this.emit('ended');
|
||||
}.bind(this);
|
||||
};
|
||||
|
||||
cc.js.extend(Audio, EventTarget);
|
||||
|
||||
/**
|
||||
* !#en Audio state.
|
||||
* !#zh 声音播放状态
|
||||
* @enum audioEngine.AudioState
|
||||
* @memberof cc
|
||||
*/
|
||||
// TODO - At present, the state is mixed with two states of users and systems, and it is best to split into two types. A "loading" should also be added to the system state.
|
||||
Audio.State = {
|
||||
/**
|
||||
* @property {Number} ERROR
|
||||
*/
|
||||
ERROR : -1,
|
||||
/**
|
||||
* @property {Number} INITIALZING
|
||||
*/
|
||||
INITIALZING: 0,
|
||||
/**
|
||||
* @property {Number} PLAYING
|
||||
*/
|
||||
PLAYING: 1,
|
||||
/**
|
||||
* @property {Number} PAUSED
|
||||
*/
|
||||
PAUSED: 2,
|
||||
/**
|
||||
* @property {Number} STOPPED
|
||||
*/
|
||||
STOPPED: 3,
|
||||
};
|
||||
|
||||
(function (proto) {
|
||||
|
||||
proto._bindEnded = function (callback) {
|
||||
callback = callback || this._onended;
|
||||
let elem = this._element;
|
||||
if (this._src && (elem instanceof HTMLAudioElement)) {
|
||||
elem.addEventListener('ended', callback);
|
||||
} else {
|
||||
elem.onended = callback;
|
||||
}
|
||||
};
|
||||
|
||||
proto._unbindEnded = function () {
|
||||
let elem = this._element;
|
||||
if (elem instanceof HTMLAudioElement) {
|
||||
elem.removeEventListener('ended', this._onended);
|
||||
} else if (elem) {
|
||||
elem.onended = null;
|
||||
}
|
||||
};
|
||||
|
||||
// proto.mount = function (elem) {
|
||||
// if (CC_DEBUG) {
|
||||
// cc.warn('Audio.mount(value) is deprecated. Please use Audio._onLoaded().');
|
||||
// }
|
||||
// };
|
||||
|
||||
proto._onLoaded = function () {
|
||||
let elem = this._src._nativeAsset;
|
||||
if (elem instanceof HTMLAudioElement) {
|
||||
// Reuse dom audio element
|
||||
if (!this._element) {
|
||||
this._element = document.createElement('audio');
|
||||
}
|
||||
this._element.src = elem.src;
|
||||
}
|
||||
else {
|
||||
this._element = new WebAudioElement(elem, this);
|
||||
}
|
||||
|
||||
this.setVolume(this._volume);
|
||||
this.setLoop(this._loop);
|
||||
if (this._nextTime !== 0) {
|
||||
this.setCurrentTime(this._nextTime);
|
||||
}
|
||||
if (this._state === Audio.State.PLAYING) {
|
||||
this.play();
|
||||
}
|
||||
else {
|
||||
this._state = Audio.State.INITIALZING;
|
||||
}
|
||||
};
|
||||
|
||||
proto.play = function () {
|
||||
// marked as playing so it will playOnLoad
|
||||
this._state = Audio.State.PLAYING;
|
||||
|
||||
if (!this._element) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._bindEnded();
|
||||
this._element.play();
|
||||
|
||||
if (!CC_QQPLAY && !CC_WECHATGAME) {
|
||||
if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO &&
|
||||
this._element.paused) {
|
||||
touchPlayList.push({ instance: this, offset: 0, audio: this._element });
|
||||
}
|
||||
|
||||
if (touchBinded) return;
|
||||
touchBinded = true;
|
||||
|
||||
// Listen to the touchstart body event and play the audio when necessary.
|
||||
cc.game.canvas.addEventListener('touchstart', function () {
|
||||
let item;
|
||||
while (item = touchPlayList.pop()) {
|
||||
item.audio.play(item.offset);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
proto.destroy = function () {
|
||||
if (CC_WECHATGAME) {
|
||||
this._element && this._element.destroy();
|
||||
}
|
||||
this._element = null;
|
||||
};
|
||||
|
||||
proto.pause = function () {
|
||||
if (!this._element || this._state !== Audio.State.PLAYING) return;
|
||||
this._unbindEnded();
|
||||
this._element.pause();
|
||||
this._state = Audio.State.PAUSED;
|
||||
};
|
||||
|
||||
proto.resume = function () {
|
||||
if (!this._element || this._state !== Audio.State.PAUSED) return;
|
||||
this._bindEnded();
|
||||
this._element.play();
|
||||
this._state = Audio.State.PLAYING;
|
||||
};
|
||||
|
||||
proto.stop = function () {
|
||||
if (!this._element) return;
|
||||
this._element.pause();
|
||||
try {
|
||||
this._element.currentTime = 0;
|
||||
} catch (error) {}
|
||||
// remove touchPlayList
|
||||
for (let i = 0; i < touchPlayList.length; i++) {
|
||||
if (touchPlayList[i].instance === this) {
|
||||
touchPlayList.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
this._unbindEnded();
|
||||
this.emit('stop');
|
||||
this._state = Audio.State.STOPPED;
|
||||
};
|
||||
|
||||
proto.setLoop = function (loop) {
|
||||
this._loop = loop;
|
||||
if (this._element) {
|
||||
this._element.loop = loop;
|
||||
}
|
||||
};
|
||||
proto.getLoop = function () {
|
||||
return this._loop;
|
||||
};
|
||||
|
||||
proto.setVolume = function (num) {
|
||||
this._volume = num;
|
||||
if (this._element) {
|
||||
this._element.volume = num;
|
||||
}
|
||||
};
|
||||
proto.getVolume = function () {
|
||||
return this._volume;
|
||||
};
|
||||
|
||||
proto.setCurrentTime = function (num) {
|
||||
if (this._element) {
|
||||
this._nextTime = 0;
|
||||
}
|
||||
else {
|
||||
this._nextTime = num;
|
||||
return;
|
||||
}
|
||||
|
||||
this._unbindEnded();
|
||||
if (!(CC_QQPLAY || CC_WECHATGAME)) {
|
||||
this._bindEnded(function () {
|
||||
this._bindEnded();
|
||||
}.bind(this));
|
||||
}
|
||||
try {
|
||||
this._element.currentTime = num;
|
||||
}
|
||||
catch (err) {
|
||||
let _element = this._element;
|
||||
if (_element.addEventListener) {
|
||||
let func = function () {
|
||||
_element.removeEventListener('loadedmetadata', func);
|
||||
_element.currentTime = num;
|
||||
};
|
||||
_element.addEventListener('loadedmetadata', func);
|
||||
}
|
||||
}
|
||||
};
|
||||
proto.getCurrentTime = function () {
|
||||
return this._element ? this._element.currentTime : 0;
|
||||
};
|
||||
|
||||
proto.getDuration = function () {
|
||||
return this._element ? this._element.duration : 0;
|
||||
};
|
||||
|
||||
proto.getState = function () {
|
||||
if (!CC_WECHATGAME) {
|
||||
let elem = this._element;
|
||||
if (elem && Audio.State.PLAYING === this._state && elem.paused) {
|
||||
this._state = Audio.State.PAUSED;
|
||||
}
|
||||
}
|
||||
return this._state;
|
||||
};
|
||||
|
||||
proto.__defineGetter__('src', function () {
|
||||
return this._src;
|
||||
});
|
||||
proto.__defineSetter__('src', function (clip) {
|
||||
this._unbindEnded();
|
||||
if (clip) {
|
||||
this._src = clip;
|
||||
if (clip.loaded) {
|
||||
this._onLoaded();
|
||||
}
|
||||
else {
|
||||
let self = this;
|
||||
clip.once('load', function () {
|
||||
if (clip === self._src) {
|
||||
self._onLoaded();
|
||||
}
|
||||
});
|
||||
cc.loader.load({
|
||||
url: clip.nativeUrl,
|
||||
// For audio, we should skip loader otherwise it will load a new audioClip.
|
||||
skips: ['Loader'],
|
||||
},
|
||||
function (err, audioNativeAsset) {
|
||||
if (err) {
|
||||
cc.error(err);
|
||||
return;
|
||||
}
|
||||
if (!clip.loaded) {
|
||||
clip._nativeAsset = audioNativeAsset;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._src = null;
|
||||
if (this._element instanceof HTMLAudioElement) {
|
||||
this._element.src = '';
|
||||
}
|
||||
else {
|
||||
this._element = null;
|
||||
}
|
||||
this._state = Audio.State.INITIALZING;
|
||||
}
|
||||
return clip;
|
||||
});
|
||||
|
||||
proto.__defineGetter__('paused', function () {
|
||||
return this._element ? this._element.paused : true;
|
||||
});
|
||||
|
||||
// setFinishCallback
|
||||
|
||||
})(Audio.prototype);
|
||||
|
||||
// Encapsulated WebAudio interface
|
||||
let WebAudioElement = function (buffer, audio) {
|
||||
this._audio = audio;
|
||||
this._context = sys.__audioSupport.context;
|
||||
this._buffer = buffer;
|
||||
|
||||
this._gainObj = this._context['createGain']();
|
||||
this._volume = 1;
|
||||
// https://www.chromestatus.com/features/5287995770929152
|
||||
if (this._gainObj['gain'].setTargetAtTime) {
|
||||
this._gainObj['gain'].setTargetAtTime(this._volume, this._context.currentTime, 0.01);
|
||||
} else {
|
||||
this._gainObj['gain'].value = 1;
|
||||
}
|
||||
this._gainObj['connect'](this._context['destination']);
|
||||
|
||||
this._loop = false;
|
||||
// The time stamp on the audio time axis when the recording begins to play.
|
||||
this._startTime = -1;
|
||||
// Record the currently playing 'Source'
|
||||
this._currentSource = null;
|
||||
// Record the time has been played
|
||||
this.playedLength = 0;
|
||||
|
||||
this._currextTimer = null;
|
||||
|
||||
this._endCallback = function () {
|
||||
if (this.onended) {
|
||||
this.onended(this);
|
||||
}
|
||||
}.bind(this);
|
||||
};
|
||||
|
||||
(function (proto) {
|
||||
proto.play = function (offset) {
|
||||
// If repeat play, you need to stop before an audio
|
||||
if (this._currentSource && !this.paused) {
|
||||
this._currentSource.onended = null;
|
||||
this._currentSource.stop(0);
|
||||
this.playedLength = 0;
|
||||
}
|
||||
|
||||
let audio = this._context["createBufferSource"]();
|
||||
audio.buffer = this._buffer;
|
||||
audio["connect"](this._gainObj);
|
||||
audio.loop = this._loop;
|
||||
|
||||
this._startTime = this._context.currentTime;
|
||||
offset = offset || this.playedLength;
|
||||
if (offset) {
|
||||
this._startTime -= offset;
|
||||
}
|
||||
let duration = this._buffer.duration;
|
||||
|
||||
let startTime = offset;
|
||||
let endTime;
|
||||
if (this._loop) {
|
||||
if (audio.start)
|
||||
audio.start(0, startTime);
|
||||
else if (audio["notoGrainOn"])
|
||||
audio["noteGrainOn"](0, startTime);
|
||||
else
|
||||
audio["noteOn"](0, startTime);
|
||||
} else {
|
||||
endTime = duration - offset;
|
||||
if (audio.start)
|
||||
audio.start(0, startTime, endTime);
|
||||
else if (audio["notoGrainOn"])
|
||||
audio["noteGrainOn"](0, startTime, endTime);
|
||||
else
|
||||
audio["noteOn"](0, startTime, endTime);
|
||||
}
|
||||
|
||||
this._currentSource = audio;
|
||||
|
||||
audio.onended = this._endCallback;
|
||||
|
||||
// If the current audio context time stamp is 0 and audio context state is suspended
|
||||
// There may be a need to touch events before you can actually start playing audio
|
||||
if ((!audio.context.state || audio.context.state === "suspended") && this._context.currentTime === 0) {
|
||||
let self = this;
|
||||
clearTimeout(this._currextTimer);
|
||||
this._currextTimer = setTimeout(function () {
|
||||
if (!(CC_QQPLAY || CC_WECHATGAME) && self._context.currentTime === 0) {
|
||||
touchPlayList.push({
|
||||
instance: self._audio,
|
||||
offset: offset,
|
||||
audio: self
|
||||
});
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
};
|
||||
|
||||
proto.pause = function () {
|
||||
clearTimeout(this._currextTimer);
|
||||
if (this.paused) return;
|
||||
// Record the time the current has been played
|
||||
this.playedLength = this._context.currentTime - this._startTime;
|
||||
// If more than the duration of the audio, Need to take the remainder
|
||||
this.playedLength %= this._buffer.duration;
|
||||
let audio = this._currentSource;
|
||||
this._currentSource = null;
|
||||
this._startTime = -1;
|
||||
if (audio)
|
||||
audio.stop(0);
|
||||
};
|
||||
|
||||
proto.__defineGetter__('paused', function () {
|
||||
// If the current audio is a loop, paused is false
|
||||
if (this._currentSource && this._currentSource.loop)
|
||||
return false;
|
||||
|
||||
// startTime default is -1
|
||||
if (this._startTime === -1)
|
||||
return true;
|
||||
|
||||
// Current time - Start playing time > Audio duration
|
||||
return this._context.currentTime - this._startTime > this._buffer.duration;
|
||||
});
|
||||
|
||||
proto.__defineGetter__('loop', function () { return this._loop; });
|
||||
proto.__defineSetter__('loop', function (bool) {
|
||||
if (this._currentSource)
|
||||
this._currentSource.loop = bool;
|
||||
|
||||
return this._loop = bool;
|
||||
});
|
||||
|
||||
proto.__defineGetter__('volume', function () {
|
||||
return this._volume;
|
||||
});
|
||||
proto.__defineSetter__('volume', function (num) {
|
||||
this._volume = num;
|
||||
if (this._gainObj['gain'].setTargetAtTime) {
|
||||
this._gainObj['gain'].setTargetAtTime(this._volume, this._context.currentTime, 0.01);
|
||||
} else {
|
||||
this._volume['gain'].value = num;
|
||||
}
|
||||
if (sys.os === sys.OS_IOS && !this.paused && this._currentSource) {
|
||||
// IOS must be stop webAudio
|
||||
this._currentSource.onended = null;
|
||||
this.pause();
|
||||
this.play();
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
proto.__defineGetter__('currentTime', function () {
|
||||
if (this.paused) {
|
||||
return this.playedLength;
|
||||
}
|
||||
// Record the time the current has been played
|
||||
this.playedLength = this._context.currentTime - this._startTime;
|
||||
// If more than the duration of the audio, Need to take the remainder
|
||||
this.playedLength %= this._buffer.duration;
|
||||
return this.playedLength;
|
||||
});
|
||||
proto.__defineSetter__('currentTime', function (num) {
|
||||
if (!this.paused) {
|
||||
this.pause();
|
||||
this.playedLength = num;
|
||||
this.play();
|
||||
} else {
|
||||
this.playedLength = num;
|
||||
}
|
||||
return num;
|
||||
});
|
||||
|
||||
proto.__defineGetter__('duration', function () {
|
||||
return this._buffer.duration;
|
||||
});
|
||||
|
||||
})(WebAudioElement.prototype);
|
||||
|
||||
module.exports = cc.Audio = Audio;
|
||||
@@ -1,115 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
const Asset = require('./CCAsset');
|
||||
const EventTarget = require('../event/event-target');
|
||||
|
||||
var LoadMode = cc.Enum({
|
||||
WEB_AUDIO: 0,
|
||||
DOM_AUDIO: 1,
|
||||
});
|
||||
|
||||
/**
|
||||
* !#en Class for audio data handling.
|
||||
* !#zh 音频资源类。
|
||||
* @class AudioClip
|
||||
* @extends Asset
|
||||
* @uses EventTarget
|
||||
*/
|
||||
var AudioClip = cc.Class({
|
||||
name: 'cc.AudioClip',
|
||||
extends: Asset,
|
||||
mixins: [EventTarget],
|
||||
|
||||
ctor () {
|
||||
this.loaded = false;
|
||||
|
||||
// the web audio buffer or <audio> element
|
||||
this._audio = null;
|
||||
},
|
||||
|
||||
properties: {
|
||||
loadMode: {
|
||||
default: LoadMode.WEB_AUDIO,
|
||||
type: LoadMode
|
||||
},
|
||||
_nativeAsset: {
|
||||
get () {
|
||||
return this._audio;
|
||||
},
|
||||
set (value) {
|
||||
this._audio = value;
|
||||
if (value) {
|
||||
this.loaded = true;
|
||||
this.emit('load');
|
||||
}
|
||||
},
|
||||
override: true
|
||||
}
|
||||
},
|
||||
|
||||
statics: {
|
||||
LoadMode: LoadMode,
|
||||
_loadByUrl: function (url, callback) {
|
||||
var item = cc.loader.getItem(url) || cc.loader.getItem(url + '?useDom=1');
|
||||
if (!item || !item.complete) {
|
||||
cc.loader.load(url, function (error, downloadUrl) {
|
||||
if (error) {
|
||||
return callback(error);
|
||||
}
|
||||
item = cc.loader.getItem(url) || cc.loader.getItem(url + '?useDom=1');
|
||||
callback(null, item.content);
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (item._owner instanceof AudioClip) {
|
||||
// preloaded and retained by audio clip
|
||||
callback(null, item._owner);
|
||||
}
|
||||
else {
|
||||
callback(null, item.content);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
destroy () {
|
||||
cc.audioEngine.uncache(this);
|
||||
this._super();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* !#zh
|
||||
* 当该资源加载成功后触发该事件
|
||||
* !#en
|
||||
* This event is emitted when the asset is loaded
|
||||
*
|
||||
* @event load
|
||||
*/
|
||||
|
||||
cc.AudioClip = AudioClip;
|
||||
module.exports = AudioClip;
|
||||
@@ -1,809 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2008-2010 Ricardo Quesada
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 Audio = require('./CCAudio');
|
||||
const AudioClip = require('../core/assets/CCAudioClip');
|
||||
const js = cc.js;
|
||||
|
||||
let _instanceId = 0;
|
||||
let _id2audio = js.createMap(true);
|
||||
let _url2id = {};
|
||||
let _audioPool = [];
|
||||
|
||||
let recycleAudio = function (audio) {
|
||||
audio._finishCallback = null;
|
||||
if (_audioPool.length < 32) {
|
||||
audio.off('ended');
|
||||
audio.off('stop');
|
||||
audio.src = null;
|
||||
_audioPool.push(audio);
|
||||
}
|
||||
else {
|
||||
audio.destroy();
|
||||
}
|
||||
};
|
||||
|
||||
let getAudioFromPath = function (path) {
|
||||
var id = _instanceId++;
|
||||
var list = _url2id[path];
|
||||
if (!list) {
|
||||
list = _url2id[path] = [];
|
||||
}
|
||||
if (audioEngine._maxAudioInstance <= list.length) {
|
||||
var oldId = list.shift();
|
||||
var oldAudio = getAudioFromId(oldId);
|
||||
// Stop will recycle audio automatically by event callback
|
||||
oldAudio.stop();
|
||||
}
|
||||
|
||||
var audio = _audioPool.pop() || new Audio();
|
||||
var callback = function () {
|
||||
var audioInList = getAudioFromId(this.id);
|
||||
if (audioInList) {
|
||||
delete _id2audio[this.id];
|
||||
var index = list.indexOf(this.id);
|
||||
cc.js.array.fastRemoveAt(list, index);
|
||||
}
|
||||
recycleAudio(this);
|
||||
};
|
||||
|
||||
audio.on('ended', function () {
|
||||
if (this._finishCallback) {
|
||||
this._finishCallback();
|
||||
}
|
||||
callback.call(this);
|
||||
}, audio);
|
||||
|
||||
audio.on('stop', callback, audio);
|
||||
audio.id = id;
|
||||
_id2audio[id] = audio;
|
||||
list.push(id);
|
||||
|
||||
return audio;
|
||||
};
|
||||
|
||||
let getAudioFromId = function (id) {
|
||||
return _id2audio[id];
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en cc.audioEngine is the singleton object, it provide simple audio APIs.
|
||||
* !#zh
|
||||
* cc.audioengine是单例对象。<br/>
|
||||
* 主要用来播放音频,播放的时候会返回一个 audioID,之后都可以通过这个 audioID 来操作这个音频对象。<br/>
|
||||
* 不使用的时候,请使用 cc.audioEngine.uncache(filePath); 进行资源释放 <br/>
|
||||
* 注意:<br/>
|
||||
* 在 Android 系统浏览器上,不同浏览器,不同版本的效果不尽相同。<br/>
|
||||
* 比如说:大多数浏览器都需要用户物理交互才可以开始播放音效,有一些不支持 WebAudio,<br/>
|
||||
* 有一些不支持多音轨播放。总之如果对音乐依赖比较强,请做尽可能多的测试。
|
||||
* @class audioEngine
|
||||
* @static
|
||||
*/
|
||||
var audioEngine = {
|
||||
|
||||
AudioState: Audio.State,
|
||||
|
||||
_maxWebAudioSize: 2097152, // 2048kb * 1024
|
||||
_maxAudioInstance: 24,
|
||||
|
||||
_id2audio: _id2audio,
|
||||
|
||||
/**
|
||||
* !#en Play audio.
|
||||
* !#zh 播放音频
|
||||
* @method play
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @param {Number} volume - Volume size.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.play(clip, false, 0.5);
|
||||
* });
|
||||
*/
|
||||
play: function (clip, loop, volume/*, profile*/) {
|
||||
var path = clip;
|
||||
var audio;
|
||||
if (typeof clip === 'string') {
|
||||
// backward compatibility since 1.10
|
||||
cc.warnID(8401, 'cc.audioEngine', 'cc.AudioClip', 'AudioClip', 'cc.AudioClip', 'audio');
|
||||
path = clip;
|
||||
// load clip
|
||||
audio = getAudioFromPath(path);
|
||||
AudioClip._loadByUrl(path, function (err, clip) {
|
||||
if (clip) {
|
||||
audio.src = clip;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (!clip) {
|
||||
return;
|
||||
}
|
||||
path = clip.nativeUrl;
|
||||
audio = getAudioFromPath(path);
|
||||
audio.src = clip;
|
||||
}
|
||||
|
||||
audio.setLoop(loop || false);
|
||||
if (typeof volume !== 'number') {
|
||||
volume = 1;
|
||||
}
|
||||
audio.setVolume(volume);
|
||||
audio.play();
|
||||
|
||||
return audio.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set audio loop.
|
||||
* !#zh 设置音频是否循环。
|
||||
* @method setLoop
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Boolean} loop - Whether cycle.
|
||||
* @example
|
||||
* cc.audioEngine.setLoop(id, true);
|
||||
*/
|
||||
setLoop: function (audioID, loop) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio || !audio.setLoop)
|
||||
return;
|
||||
audio.setLoop(loop);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio cycle state.
|
||||
* !#zh 获取音频的循环状态。
|
||||
* @method isLoop
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Boolean} Whether cycle.
|
||||
* @example
|
||||
* cc.audioEngine.isLoop(id);
|
||||
*/
|
||||
isLoop: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio || !audio.getLoop)
|
||||
return false;
|
||||
return audio.getLoop();
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the volume of audio.
|
||||
* !#zh 设置音量(0.0 ~ 1.0)。
|
||||
* @method setVolume
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0 .
|
||||
* @example
|
||||
* cc.audioEngine.setVolume(id, 0.5);
|
||||
*/
|
||||
setVolume: function (audioID, volume) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.setVolume(volume);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The volume of the music max value is 1.0,the min value is 0.0 .
|
||||
* !#zh 获取音量(0.0 ~ 1.0)。
|
||||
* @method getVolume
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getVolume(id);
|
||||
*/
|
||||
getVolume: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getVolume() : 1;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set current time
|
||||
* !#zh 设置当前的音频时间。
|
||||
* @method setCurrentTime
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Number} sec - current time.
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* cc.audioEngine.setCurrentTime(id, 2);
|
||||
*/
|
||||
setCurrentTime: function (audioID, sec) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.setCurrentTime(sec);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get current time
|
||||
* !#zh 获取当前的音频播放时间。
|
||||
* @method getCurrentTime
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number} audio current time.
|
||||
* @example
|
||||
* var time = cc.audioEngine.getCurrentTime(id);
|
||||
*/
|
||||
getCurrentTime: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getCurrentTime() : 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio duration
|
||||
* !#zh 获取音频总时长。
|
||||
* @method getDuration
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number} audio duration.
|
||||
* @example
|
||||
* var time = cc.audioEngine.getDuration(id);
|
||||
*/
|
||||
getDuration: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getDuration() : 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio state
|
||||
* !#zh 获取音频状态。
|
||||
* @method getState
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {audioEngine.AudioState} audio duration.
|
||||
* @example
|
||||
* var state = cc.audioEngine.getState(id);
|
||||
*/
|
||||
getState: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getState() : this.AudioState.ERROR;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set Audio finish callback
|
||||
* !#zh 设置一个音频结束后的回调
|
||||
* @method setFinishCallback
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Function} callback - loaded callback.
|
||||
* @example
|
||||
* cc.audioEngine.setFinishCallback(id, function () {});
|
||||
*/
|
||||
setFinishCallback: function (audioID, callback) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio)
|
||||
return;
|
||||
audio._finishCallback = callback;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause playing audio.
|
||||
* !#zh 暂停正在播放音频。
|
||||
* @method pause
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.pause(audioID);
|
||||
*/
|
||||
pause: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.pause();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
_pauseIDCache: [],
|
||||
/**
|
||||
* !#en Pause all playing audio
|
||||
* !#zh 暂停现在正在播放的所有音频。
|
||||
* @method pauseAll
|
||||
* @example
|
||||
* cc.audioEngine.pauseAll();
|
||||
*/
|
||||
pauseAll: function () {
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === Audio.State.PLAYING) {
|
||||
this._pauseIDCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume playing audio.
|
||||
* !#zh 恢复播放指定的音频。
|
||||
* @method resume
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.resume(audioID);
|
||||
*/
|
||||
resume: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.resume();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume all playing audio.
|
||||
* !#zh 恢复播放所有之前暂停的所有音频。
|
||||
* @method resumeAll
|
||||
* @example
|
||||
* cc.audioEngine.resumeAll();
|
||||
*/
|
||||
resumeAll: function () {
|
||||
for (var i = 0; i < this._pauseIDCache.length; ++i) {
|
||||
var id = this._pauseIDCache[i];
|
||||
var audio = getAudioFromId(id);
|
||||
if (audio)
|
||||
audio.resume();
|
||||
}
|
||||
this._pauseIDCache.length = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing audio.
|
||||
* !#zh 停止播放指定音频。
|
||||
* @method stop
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.stop(audioID);
|
||||
*/
|
||||
stop: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop all playing audio.
|
||||
* !#zh 停止正在播放的所有音频。
|
||||
* @method stopAll
|
||||
* @example
|
||||
* cc.audioEngine.stopAll();
|
||||
*/
|
||||
stopAll: function () {
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set up an audio can generate a few examples.
|
||||
* !#zh 设置一个音频可以设置几个实例
|
||||
* @method setMaxAudioInstance
|
||||
* @param {Number} num - a number of instances to be created from within an audio
|
||||
* @example
|
||||
* cc.audioEngine.setMaxAudioInstance(20);
|
||||
*/
|
||||
setMaxAudioInstance: function (num) {
|
||||
this._maxAudioInstance = num;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Getting audio can produce several examples.
|
||||
* !#zh 获取一个音频可以设置几个实例
|
||||
* @method getMaxAudioInstance
|
||||
* @return {Number} a - number of instances to be created from within an audio
|
||||
* @example
|
||||
* cc.audioEngine.getMaxAudioInstance();
|
||||
*/
|
||||
getMaxAudioInstance: function () {
|
||||
return this._maxAudioInstance;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Unload the preloaded audio from internal buffer.
|
||||
* !#zh 卸载预加载的音频。
|
||||
* @method uncache
|
||||
* @param {AudioClip} clip
|
||||
* @example
|
||||
* cc.audioEngine.uncache(filePath);
|
||||
*/
|
||||
uncache: function (clip) {
|
||||
var filePath = clip;
|
||||
if (typeof clip === 'string') {
|
||||
// backward compatibility since 1.10
|
||||
cc.warnID(8401, 'cc.audioEngine', 'cc.AudioClip', 'AudioClip', 'cc.AudioClip', 'audio');
|
||||
filePath = clip;
|
||||
}
|
||||
else {
|
||||
if (!clip) {
|
||||
return;
|
||||
}
|
||||
filePath = clip.nativeUrl;
|
||||
}
|
||||
|
||||
var list = _url2id[filePath];
|
||||
if (!list) return;
|
||||
while (list.length > 0) {
|
||||
var id = list.pop();
|
||||
var audio = _id2audio[id];
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
delete _id2audio[id];
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Unload all audio from internal buffer.
|
||||
* !#zh 卸载所有音频。
|
||||
* @method uncacheAll
|
||||
* @example
|
||||
* cc.audioEngine.uncacheAll();
|
||||
*/
|
||||
uncacheAll: function () {
|
||||
this.stopAll();
|
||||
let audio;
|
||||
for (let id in _id2audio) {
|
||||
audio = _id2audio[id];
|
||||
if (audio) {
|
||||
audio.destroy();
|
||||
}
|
||||
}
|
||||
while (audio = _audioPool.pop()) {
|
||||
audio.destroy();
|
||||
}
|
||||
_id2audio = js.createMap(true);
|
||||
_url2id = {};
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Gets an audio profile by name.
|
||||
*
|
||||
* @param profileName A name of audio profile.
|
||||
* @return The audio profile.
|
||||
*/
|
||||
getProfile: function (profileName) {},
|
||||
|
||||
/**
|
||||
* !#en Preload audio file.
|
||||
* !#zh 预加载一个音频
|
||||
* @method preload
|
||||
* @param {String} filePath - The file path of an audio.
|
||||
* @param {Function} [callback] - The callback of an audio.
|
||||
* @example
|
||||
* cc.audioEngine.preload(path);
|
||||
* @deprecated `cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please.
|
||||
*/
|
||||
preload: function (filePath, callback) {
|
||||
if (CC_DEBUG) {
|
||||
cc.warn('`cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please.');
|
||||
}
|
||||
|
||||
cc.loader.load(filePath, callback && function (error) {
|
||||
if (!error) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set a size, the unit is KB. Over this size is directly resolved into DOM nodes.
|
||||
* !#zh 设置一个以 KB 为单位的尺寸,大于这个尺寸的音频在加载的时候会强制使用 dom 方式加载
|
||||
* @method setMaxWebAudioSize
|
||||
* @param {Number} kb - The file path of an audio.
|
||||
* @example
|
||||
* cc.audioEngine.setMaxWebAudioSize(300);
|
||||
*/
|
||||
// Because webAudio takes up too much memory,So allow users to manually choose
|
||||
setMaxWebAudioSize: function (kb) {
|
||||
this._maxWebAudioSize = kb * 1024;
|
||||
},
|
||||
|
||||
_breakCache: null,
|
||||
_break: function () {
|
||||
this._breakCache = [];
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === Audio.State.PLAYING) {
|
||||
this._breakCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_restore: function () {
|
||||
if (!this._breakCache) return;
|
||||
|
||||
while (this._breakCache.length > 0) {
|
||||
var id = this._breakCache.pop();
|
||||
var audio = getAudioFromId(id);
|
||||
if (audio && audio.resume)
|
||||
audio.resume();
|
||||
}
|
||||
this._breakCache = null;
|
||||
},
|
||||
|
||||
///////////////////////////////
|
||||
// Classification of interface
|
||||
|
||||
_music: {
|
||||
id: -1,
|
||||
loop: false,
|
||||
volume: 1,
|
||||
},
|
||||
|
||||
_effect: {
|
||||
volume: 1,
|
||||
pauseCache: [],
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Play background music
|
||||
* !#zh 播放背景音乐
|
||||
* @method playMusic
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.playMusic(clip, false);
|
||||
* });
|
||||
*/
|
||||
playMusic: function (clip, loop) {
|
||||
var music = this._music;
|
||||
this.stop(music.id);
|
||||
music.id = this.play(clip, loop, music.volume);
|
||||
music.loop = loop;
|
||||
return music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop background music.
|
||||
* !#zh 停止播放背景音乐。
|
||||
* @method stopMusic
|
||||
* @example
|
||||
* cc.audioEngine.stopMusic();
|
||||
*/
|
||||
stopMusic: function () {
|
||||
this.stop(this._music.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause the background music.
|
||||
* !#zh 暂停播放背景音乐。
|
||||
* @method pauseMusic
|
||||
* @example
|
||||
* cc.audioEngine.pauseMusic();
|
||||
*/
|
||||
pauseMusic: function () {
|
||||
this.pause(this._music.id);
|
||||
return this._music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume playing background music.
|
||||
* !#zh 恢复播放背景音乐。
|
||||
* @method resumeMusic
|
||||
* @example
|
||||
* cc.audioEngine.resumeMusic();
|
||||
*/
|
||||
resumeMusic: function () {
|
||||
this.resume(this._music.id);
|
||||
return this._music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get the volume(0.0 ~ 1.0).
|
||||
* !#zh 获取音量(0.0 ~ 1.0)。
|
||||
* @method getMusicVolume
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getMusicVolume();
|
||||
*/
|
||||
getMusicVolume: function () {
|
||||
return this._music.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the background music volume.
|
||||
* !#zh 设置背景音乐音量(0.0 ~ 1.0)。
|
||||
* @method setMusicVolume
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0.
|
||||
* @example
|
||||
* cc.audioEngine.setMusicVolume(0.5);
|
||||
*/
|
||||
setMusicVolume: function (volume) {
|
||||
var music = this._music;
|
||||
music.volume = volume;
|
||||
this.setVolume(music.id, music.volume);
|
||||
return music.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Background music playing state
|
||||
* !#zh 背景音乐是否正在播放
|
||||
* @method isMusicPlaying
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* cc.audioEngine.isMusicPlaying();
|
||||
*/
|
||||
isMusicPlaying: function () {
|
||||
return this.getState(this._music.id) === this.AudioState.PLAYING;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Play effect audio.
|
||||
* !#zh 播放音效
|
||||
* @method playEffect
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.playEffect(clip, false);
|
||||
* });
|
||||
*/
|
||||
playEffect: function (clip, loop) {
|
||||
return this.play(clip, loop || false, this._effect.volume);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the volume of effect audio.
|
||||
* !#zh 设置音效音量(0.0 ~ 1.0)。
|
||||
* @method setEffectsVolume
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0.
|
||||
* @example
|
||||
* cc.audioEngine.setEffectsVolume(0.5);
|
||||
*/
|
||||
setEffectsVolume: function (volume) {
|
||||
var musicId = this._music.id;
|
||||
this._effect.volume = volume;
|
||||
for (var id in _id2audio) {
|
||||
if (id === musicId) continue;
|
||||
audioEngine.setVolume(id, volume);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The volume of the effect audio max value is 1.0,the min value is 0.0 .
|
||||
* !#zh 获取音效音量(0.0 ~ 1.0)。
|
||||
* @method getEffectsVolume
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getEffectsVolume();
|
||||
*/
|
||||
getEffectsVolume: function () {
|
||||
return this._effect.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause effect audio.
|
||||
* !#zh 暂停播放音效。
|
||||
* @method pauseEffect
|
||||
* @param {Number} audioID - audio id.
|
||||
* @example
|
||||
* cc.audioEngine.pauseEffect(audioID);
|
||||
*/
|
||||
pauseEffect: function (audioID) {
|
||||
return this.pause(audioID);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing all the sound effects.
|
||||
* !#zh 暂停播放所有音效。
|
||||
* @method pauseAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.pauseAllEffects();
|
||||
*/
|
||||
pauseAllEffects: function () {
|
||||
var musicId = this._music.id;
|
||||
var effect = this._effect;
|
||||
effect.pauseCache.length = 0;
|
||||
|
||||
for (var id in _id2audio) {
|
||||
if (id === musicId) continue;
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === this.AudioState.PLAYING) {
|
||||
effect.pauseCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume effect audio.
|
||||
* !#zh 恢复播放音效音频。
|
||||
* @method resumeEffect
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.resumeEffect(audioID);
|
||||
*/
|
||||
resumeEffect: function (id) {
|
||||
this.resume(id);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume all effect audio.
|
||||
* !#zh 恢复播放所有之前暂停的音效。
|
||||
* @method resumeAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.resumeAllEffects();
|
||||
*/
|
||||
resumeAllEffects: function () {
|
||||
var pauseIDCache = this._effect.pauseCache;
|
||||
for (var i = 0; i < pauseIDCache.length; ++i) {
|
||||
var id = pauseIDCache[i];
|
||||
var audio = _id2audio[id];
|
||||
if (audio)
|
||||
audio.resume();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing the effect audio.
|
||||
* !#zh 停止播放音效。
|
||||
* @method stopEffect
|
||||
* @param {Number} audioID - audio id.
|
||||
* @example
|
||||
* cc.audioEngine.stopEffect(id);
|
||||
*/
|
||||
stopEffect: function (audioID) {
|
||||
return this.stop(audioID);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing all the effects.
|
||||
* !#zh 停止播放所有音效。
|
||||
* @method stopAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.stopAllEffects();
|
||||
*/
|
||||
stopAllEffects: function () {
|
||||
var musicId = this._music.id;
|
||||
for (var id in _id2audio) {
|
||||
if (id === musicId) continue;
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === audioEngine.AudioState.PLAYING) {
|
||||
audio.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cc.audioEngine = audioEngine;
|
||||
@@ -1,340 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
import * as misc from '../core/utils/misc';
|
||||
import { Component } from './component';
|
||||
import AudioClip from '../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
|
||||
},
|
||||
|
||||
/**
|
||||
* !#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 (typeof value === 'string') {
|
||||
// backward compatibility since 1.10
|
||||
cc.warnID(8401, 'cc.AudioSource', 'cc.AudioClip', 'AudioClip', 'cc.AudioClip', 'audio');
|
||||
let self = this;
|
||||
AudioClip._loadByUrl(value, function (err, clip) {
|
||||
if (clip) {
|
||||
self.clip = clip;
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (value === this._clip) {
|
||||
return;
|
||||
}
|
||||
this._clip = value;
|
||||
this.audio.stop();
|
||||
if (this.preload) {
|
||||
this.audio.src = this._clip;
|
||||
}
|
||||
},
|
||||
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
|
||||
},
|
||||
|
||||
preload: {
|
||||
default: false,
|
||||
animatable: false
|
||||
}
|
||||
},
|
||||
|
||||
_ensureDataLoaded () {
|
||||
if (this.audio.src !== this._clip) {
|
||||
this.audio.src = this._clip;
|
||||
}
|
||||
},
|
||||
|
||||
_pausedCallback: function () {
|
||||
var audio = this.audio;
|
||||
if (audio.paused) return;
|
||||
this.audio.pause();
|
||||
this._pausedFlag = true;
|
||||
},
|
||||
|
||||
_restoreCallback: function () {
|
||||
if (this._pausedFlag) {
|
||||
this.audio.resume();
|
||||
}
|
||||
this._pausedFlag = false;
|
||||
},
|
||||
|
||||
onLoad: function () {
|
||||
this.audio.setVolume(this._mute ? 0 : this._volume);
|
||||
this.audio.setLoop(this._loop);
|
||||
},
|
||||
|
||||
onEnable: function () {
|
||||
if (this.preload) {
|
||||
this.audio.src = this._clip;
|
||||
}
|
||||
if (this.playOnLoad) {
|
||||
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.stop();
|
||||
this.audio.destroy();
|
||||
cc.audioEngine.uncache(this._clip);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Plays the clip.
|
||||
* !#zh 播放音频剪辑。
|
||||
* @method play
|
||||
*/
|
||||
play: function () {
|
||||
if ( !this._clip ) return;
|
||||
|
||||
var audio = this.audio;
|
||||
if (this._clip.loaded) {
|
||||
audio.stop();
|
||||
}
|
||||
this._ensureDataLoaded();
|
||||
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._ensureDataLoaded();
|
||||
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;
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
import sys from '../core/platform/CCSys';
|
||||
import * as debug from '../core/platform/CCDebug';
|
||||
import { AudioClip, AudioSourceType } from '../3d/assets/audio-clip';
|
||||
import { AudioClip, AudioType } from '../3d/assets/audio/clip';
|
||||
|
||||
var __audioSupport = sys.__audioSupport;
|
||||
var formatSupport = __audioSupport.format;
|
||||
@@ -109,7 +109,7 @@ export default function downloadAudio (item, callback) {
|
||||
else {
|
||||
var loadByDeserializedAudio = item._owner instanceof AudioClip;
|
||||
if (loadByDeserializedAudio) {
|
||||
loader = (item._owner.loadMode === AudioSourceType.WEB_AUDIO) ? loadWebAudio : loadDomAudio;
|
||||
loader = (item._owner.loadMode === AudioType.WEB_AUDIO) ? loadWebAudio : loadDomAudio;
|
||||
}
|
||||
else {
|
||||
loader = (item.urlParam && item.urlParam['useDom']) ? loadDomAudio : loadWebAudio;
|
||||
|
||||
@@ -12,7 +12,9 @@ import { GFXDevice } from '../../gfx/device';
|
||||
import { GFXPipelineLayout } from '../../gfx/pipeline-layout';
|
||||
import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState,
|
||||
GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state';
|
||||
import { GFXRenderPass } from '../../gfx/render-pass';
|
||||
import { GFXSampler } from '../../gfx/sampler';
|
||||
import { GFXShader } from '../../gfx/shader';
|
||||
import { GFXTextureView } from '../../gfx/texture-view';
|
||||
import { RenderPassStage, RenderPriority } from '../../pipeline/define';
|
||||
import { RenderPipeline } from '../../pipeline/render-pipeline';
|
||||
@@ -115,6 +117,8 @@ export class Pass {
|
||||
protected _defines: IDefineMap = {};
|
||||
// external references
|
||||
protected _device: GFXDevice;
|
||||
protected _renderPass: GFXRenderPass | null = null;
|
||||
protected _shader: GFXShader | null = null;
|
||||
|
||||
public constructor (device: GFXDevice) {
|
||||
this._device = device;
|
||||
@@ -173,6 +177,8 @@ export class Pass {
|
||||
if (texture) { this._textureViews[u.binding] = texture.getGFXTextureView()!; }
|
||||
else { console.warn('illegal texture default value ' + texName); }
|
||||
}
|
||||
|
||||
this.tryCompile();
|
||||
}
|
||||
|
||||
public getHandle (name: string) {
|
||||
@@ -268,12 +274,13 @@ export class Pass {
|
||||
}
|
||||
}
|
||||
|
||||
public createPipelineState () {
|
||||
const pipeline = cc.director.root.pipeline as RenderPipeline;
|
||||
const renderPass = pipeline.getRenderPass(this._stage);
|
||||
if (!renderPass) { console.warn(`illegal pass stage.`); return; }
|
||||
const shader = programLib.getGFXShader(this._device, this._programName, this._defines, pipeline);
|
||||
if (!shader) { console.warn(`create shader ${this._programName} failed`); return; }
|
||||
public tryCompile () {
|
||||
const pipeline = cc.director.root.pipeline as RenderPipeline | null;
|
||||
if (!pipeline) { return false; }
|
||||
this._renderPass = pipeline.getRenderPass(this._stage);
|
||||
if (!this._renderPass) { console.warn(`illegal pass stage.`); return false; }
|
||||
this._shader = programLib.getGFXShader(this._device, this._programName, this._defines, pipeline);
|
||||
if (!this._shader) { console.warn(`create shader ${this._programName} failed`); return false; }
|
||||
if (!this._bindings.length) {
|
||||
this._bindings = this._shaderInfo!.blocks.map((u) =>
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.UNIFORM_BUFFER }),
|
||||
@@ -281,6 +288,11 @@ export class Pass {
|
||||
({ name: u.name, binding: u.binding, type: GFXBindingType.SAMPLER }),
|
||||
));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public createPipelineState () {
|
||||
if ((!this._renderPass || !this._shader || !this._bindings.length) && !this.tryCompile()) { return; }
|
||||
// bind resources
|
||||
const bindingLayout = this._device.createBindingLayout({ bindings: this._bindings });
|
||||
for (const b of Object.keys(this._buffers)) {
|
||||
@@ -293,7 +305,7 @@ export class Pass {
|
||||
bindingLayout.bindTextureView(parseInt(t), this._textureViews[t]);
|
||||
}
|
||||
// bind pipeline builtins
|
||||
const source = pipeline.globalBindings;
|
||||
const source = cc.director.root.pipeline.globalBindings;
|
||||
const target = this._shaderInfo!.builtins;
|
||||
for (const b of target.blocks) {
|
||||
const info = source.get(b);
|
||||
@@ -315,8 +327,9 @@ export class Pass {
|
||||
is: new GFXInputState(),
|
||||
layout: pipelineLayout,
|
||||
primitive: this._primitive,
|
||||
renderPass: this._renderPass!,
|
||||
rs: this._rs,
|
||||
renderPass, shader,
|
||||
shader: this._shader!,
|
||||
});
|
||||
this._resources.push({ bindingLayout, pipelineLayout, pipelineState });
|
||||
return pipelineState;
|
||||
|
||||
@@ -89,8 +89,8 @@ class Node extends BaseNode {
|
||||
return this._layer;
|
||||
}
|
||||
|
||||
get uiTransfromComp(){
|
||||
if(!this._uiTransfromComp){
|
||||
get uiTransfromComp () {
|
||||
if (!this._uiTransfromComp){
|
||||
this._uiTransfromComp = this.getComponent(UITransformComponent);
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ class Node extends BaseNode {
|
||||
}
|
||||
|
||||
// NOTE: don't set it manually
|
||||
set uiTransfromComp(value) {
|
||||
set uiTransfromComp (value) {
|
||||
this._uiTransfromComp = value;
|
||||
}
|
||||
|
||||
|
||||
+19
-4
@@ -57,6 +57,16 @@
|
||||
cameraNode.addComponent(cc.CameraComponent);
|
||||
cameraNode.addComponent(FirstPersonCamera);
|
||||
|
||||
const n1 = new cc.Node('Audio Source 1');
|
||||
n1.parent = scene;
|
||||
const s1 = n1.addComponent(cc.AudioSourceComponent);
|
||||
const n2 = new cc.Node('Audio Source 2');
|
||||
n2.parent = scene;
|
||||
const s2 = n2.addComponent(cc.AudioSourceComponent);
|
||||
s2.loop = true;
|
||||
s2.volume = 0.5;
|
||||
s2.playOnAwake = false;
|
||||
|
||||
cc.AssetLibrary.init({
|
||||
libraryPath: 'resources',
|
||||
rawAssetsBase: 'resources',
|
||||
@@ -67,11 +77,16 @@
|
||||
}
|
||||
}
|
||||
});
|
||||
cc.loader.loadRes('nocturne', cc.AudioClip, (err, asset) => {
|
||||
debugger
|
||||
});
|
||||
|
||||
cc.director.runSceneImmediate(scene);
|
||||
let jobsRemaining = 2;
|
||||
cc.loader.loadRes('morceaux', cc.AudioClip, (err, asset) => {
|
||||
s1.clip = asset;
|
||||
if (!--jobsRemaining) cc.director.runSceneImmediate(scene);
|
||||
});
|
||||
cc.loader.loadRes('nocturne', cc.AudioClip, (err, asset) => {
|
||||
s2.clip = asset;
|
||||
if (!--jobsRemaining) cc.director.runSceneImmediate(scene);
|
||||
});
|
||||
}
|
||||
cc.game.run({ id: 'canvas' }, onStart);
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user