media_player_wrapper
Version:
A React Native wrapper for live audio streaming.
107 lines (92 loc) • 2.92 kB
JavaScript
import {
NativeModules,
DeviceEventEmitter,
NativeEventEmitter,
Platform
} from "react-native";
// Getting the exported native player manager
const RNMediaLibrary =
Platform.OS === "ios"
? NativeModules.StreamingManager
: NativeModules.RNMediaLibrary;
const EventEmitter =
Platform.OS === "ios"
? new NativeEventEmitter(RNMediaLibrary)
: DeviceEventEmitter;
if (EventEmitter != undefined) {
// Adding listener when the streamer is connected
EventEmitter.addListener("CB_CONNECT", msg => {
// console.log("==================CB_CONNECT==================");
if (onConnect !== undefined) onConnect();
});
// Adding listener when the playback state is changed
EventEmitter.addListener("CB_PLAYBACKSTATE", msg => {
// console.log("==================CB_PLAYBACKSTATE==================");
//console.log(msg);
if (onPlaybackStateChanged !== undefined) onPlaybackStateChanged(msg.STATE);
});
// Adding listener when the volume value has changed
EventEmitter.addListener("CB_VOLUMECHANGED", msg => {
// console.log("==================CB_VOLUMECHANGED==================");
//console.log(msg);
if (onVolumeChanged !== undefined) onVolumeChanged(msg.VOLUME);
});
}
var onConnect, onPlaybackStateChanged, onVolumeChanged;
const MediaLibrary = {
// PLayer methods startOverWithURL, play, pause, forward, ...
startOverWithURL(url) {
if (Platform.OS === "ios"){
RNMediaLibrary.connectWithURL(url);
}else{
RNMediaLibrary.connect(url);
}
},
play(filePath, fileExtension, songName, songArtist, artUrl) {
Platform.OS === "ios"
? RNMediaLibrary.play(filePath)
: RNMediaLibrary.play();
},
pause() {
RNMediaLibrary.pause();
},
forward() {
RNMediaLibrary.forward();
},
rewind() {
RNMediaLibrary.rewind();
},
updateLockScreen(songName, showName, imageUrl) {
if (Platform.OS === "ios") {
RNMediaLibrary.postNotification(songName, showName, imageUrl);
} else {
RNMediaLibrary.updateMetaData(songName, showName, imageUrl);
}
},
getVolume(volumeLevelCB) {
RNMediaLibrary.getVolume(volume => {
// //console.log("original volume - getVolume", volume);
volumeLevelCB(volume);
// //console.log("origal volume", volume);
});
},
setVolume(volumeLevel) {
// //console.log("RNMediaLibrary.setVolume", volumeLevel);
RNMediaLibrary.setVolume(volumeLevel);
},
getPlaybackState(stateCB) {
RNMediaLibrary.isPlaying(state => {
stateCB(state);
});
},
subscribeOnConnect(onConnectCallback) {
onConnect = onConnectCallback;
},
subscribeOnPlaybackStateChanged(onPlaybackStateChangedCallback) {
onPlaybackStateChanged = onPlaybackStateChangedCallback;
},
subscribeOnVolumeChanged(onVolumeChangedCallback) {
onVolumeChanged = onVolumeChangedCallback;
}
};
module.exports = MediaLibrary;