react-native-omplayer
Version:
video player for react-native
113 lines (94 loc) • 4.06 kB
JavaScript
/**
* Created by lcg on 2017/6/16.
*/
import { Platform, NativeModules, NativeEventEmitter } from 'react-native';
const OMAudioPlayerManager = Platform.OS === 'android' ? NativeModules.OMAudioPlayerManagerTc : NativeModules.OMAudioPlayerManager;
const OMAudioPlayerEventEmitter = new NativeEventEmitter(OMAudioPlayerManager);
class AudioEventManager {
constructor(){
this._audioPlayers = {};
this._loadStartListener = this._loadStartListener || OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioLoadStart,this.onLoadStart.bind(this));
this._loadListener = this._loadListener || OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioLoad,this.onLoad.bind(this));
this._errorListener = this._errorListener || OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioError,this.onError.bind(this));
this._progressListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioProgress,this.onProgress.bind(this));
this._SeekListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioSeek,this.onSeek.bind(this));
this._endListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMAudioEnd,this.onEnd.bind(this));
this._readyForPlayListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMReadyForPlay,this.onReadyForPlay.bind(this));
this._stalledListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMPlaybackStalled,this.onStalled.bind(this));
this._resumeListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMPlaybackResume,this.onResume.bind(this));
this._rateChangeListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMPlaybackRateChange,this.onRateChange.bind(this));
this._playerErrorListener = OMAudioPlayerEventEmitter.addListener(OMAudioPlayerManager.OMPlayerError,this.onPlayerError.bind(this));
}
addAudio(id, audio){
this._audioPlayers[id] = audio;
}
removeAudio(id){
delete this._audioPlayers[id];
}
onLoadStart(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onLoadStart(event);
}
onLoad(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onLoad(event);
}
onLoadStalled(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onLoadStalled(event);
}
onLoadResume(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onLoadResume(event);
}
onError(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onError(event);
}
onProgress(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onProgress(event);
}
onSeek(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onSeek(event);
}
onEnd(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onEnd(event);
}
onReadyForPlay(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onReadyForPlay(event);
}
onStalled(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onStalled(event);
}
onResume(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onResume(event);
}
onRateChange(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onRateChange(event);
}
onPlayerError(event){
const { id } = event;
const audio = this._audioPlayers[id];
audio && audio.onPlayerError(event);
}
}
export default new AudioEventManager();