wechat audio player (#4569)

This commit is contained in:
Yun Hsiao Wu
2019-06-06 17:39:32 +08:00
committed by Weiliang Wang
parent b2dd74520b
commit de906d169d
4 changed files with 31 additions and 68 deletions
+5
View File
@@ -29,6 +29,7 @@ import { Enum } from '../../../core/value-types';
import { AudioPlayer, PlayingState } from './player';
import { AudioPlayerDOM } from './player-dom';
import { AudioPlayerWeb } from './player-web';
import { AudioPlayerWX } from './player-wx';
export const AudioType = Enum({
UNKNOWN_AUDIO: -1,
@@ -47,6 +48,9 @@ export class AudioClip extends Asset {
if (clip instanceof AudioBuffer) {
ctor = AudioPlayerWeb;
this._loadMode = AudioType.WEB_AUDIO;
} else if (CC_WECHATGAME) {
ctor = AudioPlayerWX;
this._loadMode = AudioType.WX_GAME_AUDIO;
} else {
ctor = AudioPlayerDOM;
this._loadMode = AudioType.DOM_AUDIO;
@@ -77,6 +81,7 @@ export class AudioClip extends Asset {
public static PlayingState = PlayingState;
public static AudioType = AudioType;
public static preventDeferredLoadDependents = true;
@property // we serialize this because it's unavailable at runtime on some platforms
protected _duration = 0;
@property({
-1
View File
@@ -28,7 +28,6 @@ import { AudioPlayer, IAudioInfo, PlayingState } from './player';
export class AudioPlayerDOM extends AudioPlayer {
protected _volume = 1;
protected _loop = false;
protected _currentTimer = 0;
protected _oneShoting = false;
protected _audio: HTMLAudioElement;
+1 -1
View File
@@ -34,7 +34,7 @@ export class AudioPlayerWeb extends AudioPlayer {
protected _volume = 1;
protected _loop = false;
protected _currentTimer = 0;
protected _audio: AudioBuffer | null = null;
protected _audio: AudioBuffer;
private _context: AudioContext;
private _sourceNode: AudioBufferSourceNode;
+25 -66
View File
@@ -25,66 +25,34 @@
import { _decorator } from '../../../core/data/index';
const { ccclass } = _decorator;
import { AudioClip, AudioType } from './clip';
import { PlayingState } from './player';
import { AudioPlayer, IAudioInfo, PlayingState } from './player';
/**
* WeChat audio player.
* https://developers.weixin.qq.com/minigame/dev/guide/base-ability/audio.html
*/
@ccclass('cc.WxGameAudioClip')
export default class WxGameAudioClip extends AudioClip {
private _volume: number;
private _loop: boolean;
private _oneShoting: boolean;
private _currentTime: number;
private _state = PlayingState.STOPPED;
constructor () {
super();
this._loadMode = AudioType.WX_GAME_AUDIO;
this._volume = 1;
this._loop = false;
this._oneShoting = false;
this._currentTime = 0.0;
}
@ccclass('cc.AudioPlayerWX')
export class AudioPlayerWX extends AudioPlayer {
protected _volume = 1;
protected _loop = false;
protected _oneShoting = false;
protected _audio: any;
public setNativeAsset (clip, info) {
// typeof clip === innerAudioContext
if (!clip) {
console.warn('There is no audio in the current clip');
return;
}
super.setNativeAsset(clip, info);
this._initEventsListener();
clip.volume = this._volume;
clip.loop = this._loop;
}
public _initEventsListener () {
constructor (info: IAudioInfo) {
super(info);
this._audio = info.clip;
this._audio.obeyMuteSwitch = false;
this._audio.onPlay(() => {
this._state = PlayingState.PLAYING;
this._currentTime = this._audio.currentTime;
// @ts-ignore
this.emit('started');
this._eventTarget.emit('started');
});
this._audio.onPause(() => {
this._state = PlayingState.STOPPED;
this._oneShoting = false;
});
this._audio.onStop(() => {
this._state = PlayingState.STOPPED;
this._oneShoting = false;
this._currentTime = 0;
});
this._audio.onEnded(() => {
this._state = PlayingState.STOPPED;
this._currentTime = 0;
// @ts-ignore
this.emit('ended');
this._eventTarget.emit('ended');
if (this._oneShoting) {
this._audio.volume = this._volume;
this._audio.loop = this._loop;
@@ -94,31 +62,22 @@ export default class WxGameAudioClip extends AudioClip {
}
public play () {
if (!this._audio || this._state === PlayingState.PLAYING) {
return;
}
if (!this._audio || this._state === PlayingState.PLAYING) { return; }
this._audio.play();
}
public pause () {
if (this._state !== PlayingState.PLAYING) {
return;
}
if (!this._audio || this._state !== PlayingState.PLAYING) { return; }
this._audio.pause();
}
public stop () {
if (this._state === PlayingState.STOPPED) {
return;
}
if (!this._audio) { return; }
this._audio.stop();
}
public playOneShot (volume = 1) {
/* HTMLMediaElement doesn't support multiple playback at the
/* InnerAudioContext doesn't support multiple playback at the
same time so here we fall back to re-start style approach */
if (!this._audio) { return; }
this._audio.volume = volume;
@@ -129,12 +88,12 @@ export default class WxGameAudioClip extends AudioClip {
}
public getCurrentTime () {
return this._currentTime;
return this._audio ? this._audio.currentTime : 0;
}
public setCurrentTime (val) {
this._currentTime = val;
this._audio.seek(this._currentTime);
public setCurrentTime (val: number) {
if (!this._audio) { return; }
this._audio.seek(val);
}
public getDuration () {
@@ -145,19 +104,19 @@ export default class WxGameAudioClip extends AudioClip {
return this._audio ? this._audio.volume : this._volume;
}
public setVolume (val) {
public setVolume (val: number, immediate: boolean) {
this._volume = val;
if (this._audio) { this._audio.volume = val; }
if (this._audio && this._audio.volume !== undefined) { this._audio.volume = val; }
}
public getLoop () {
return this._loop;
}
public setLoop (val) {
public setLoop (val: boolean) {
this._loop = val;
if (this._audio) { this._audio.loop = val; }
}
}
cc.WxGameAudioClip = WxGameAudioClip;
cc.AudioPlayerWX = AudioPlayerWX;