react-native-omplayer
Version:
video player for react-native
102 lines (84 loc) • 3.33 kB
JavaScript
/**
* Created by lcg on 2017/9/20.
*/
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
class RemoteControl {
constructor() {
this.listeners = [];
if (Platform.OS === 'ios') {
this.nativeRemoteControl = NativeModules.RemoteControlManager;
this.nativeEventEmitter = new NativeEventEmitter(NativeModules.RemoteControlManager);
this._playListener = this.nativeEventEmitter.addListener(this.nativeRemoteControl.EVENT_REMOTE_CONTROL_PLAY, this._play.bind(this));
this._pauseListener = this.nativeEventEmitter.addListener(this.nativeRemoteControl.EVENT_REMOTE_CONTROL_PAUSE, this._pause.bind(this));
this._prevListener = this.nativeEventEmitter.addListener(this.nativeRemoteControl.EVENT_REMOTE_CONTROL_PREV, this._prev.bind(this));
this._nextListener = this.nativeEventEmitter.addListener(this.nativeRemoteControl.EVENT_REMOTE_CONTROL_NEXT, this._next.bind(this));
}
}
addListener(listener) {
if (this.listeners.indexOf(listener) === -1 && listener) {
this.listeners.push(listener);
}
}
removeListener(listener) {
this.listeners.splice(this.listeners.indexOf(listener), 1);
}
enableControl(controlName, enabled) {
if (Platform.OS === 'ios') {
this.nativeRemoteControl.enableControl(controlName, enabled);
}
}
setNowPlayingInfo(info) {
if (Platform.OS === 'ios') {
this.nativeRemoteControl.setNowPlayingInfo(info);
}
}
updateNowPlayingInfo(info) {
if (Platform.OS === 'ios') {
this.nativeRemoteControl.updateNowPlayingInfo(info);
}
}
resetNowPlayingInfo() {
if (Platform.OS === 'ios') {
this.nativeRemoteControl.resetNowPlayingInfo();
}
}
_play() {
this.listeners.map(listener => {
listener.onPlay && listener.onPlay();
});
}
_pause() {
this.listeners.map(listener => {
listener.onPause && listener.onPause();
});
}
_prev() {
this.listeners.map(listener => {
listener.onPrev && listener.onPrev();
});
}
_next() {
this.listeners.map(listener => {
listener.onNext && listener.onNext();
});
}
}
export default new RemoteControl();
const MediaPropName = {};
const ControlName = {};
if (Platform.OS === 'ios') {
MediaPropName.Title = NativeModules.RemoteControlManager.MediaPropTitle;
MediaPropName.Artist = NativeModules.RemoteControlManager.MediaPropArtist;
MediaPropName.Album = NativeModules.RemoteControlManager.MediaPropAlbum;
MediaPropName.Artwork = NativeModules.RemoteControlManager.MediaPropArtwork;
MediaPropName.Duration = NativeModules.RemoteControlManager.MediaPropDuration;
MediaPropName.Elapsed = NativeModules.RemoteControlManager.MediaPropElapsed;
ControlName.Play = NativeModules.RemoteControlManager.REMOTE_CONTROL_PLAY;
ControlName.Pause = NativeModules.RemoteControlManager.REMOTE_CONTROL_PAUSE;
ControlName.Prev = NativeModules.RemoteControlManager.REMOTE_CONTROL_Prev;
ControlName.Next = NativeModules.RemoteControlManager.REMOTE_CONTROL_Next;
}
export {
MediaPropName,
ControlName
}