forked from tlenclos/react-native-audio-streaming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreamingPlayer.js
More file actions
136 lines (134 loc) · 4.38 KB
/
streamingPlayer.js
File metadata and controls
136 lines (134 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
NativeModules,
DeviceEventEmitter,
Platform
} from 'react-native';
import EventEmitter from 'es2015-event-emitter';
const { ReactNativeAudioStreaming } = NativeModules;
let NATIVE_INSTANCE_COUNTER = 0;
let instanceMap = {};
function broadcastToAllInstances(evtName,evtData){
Object.keys(instanceMap).map((playerId) => {
instanceMap[playerId].trigger(evtName,evtData);
});
};
function subscribeGlobalAudioEvents(){
DeviceEventEmitter.addListener('AudioRouteInterruptionEvent',
broadcastToAllInstances.bind(null,'AudioRouteInterruptionEvent')
);
DeviceEventEmitter.addListener('AudioSessionInterruptionEvent',
broadcastToAllInstances.bind(null,'AudioSessionInterruptionEvent')
);
DeviceEventEmitter.addListener('RemoteControlEvents',
broadcastToAllInstances.bind(null,'RemoteControlEvents')
);
DeviceEventEmitter.addListener(
'AudioBridgeEvent', (evt) => {
if('playerId' in evt && instanceMap[evt.playerId] !== undefined){
instanceMap[evt.playerId].dispatchAudioEvent(evt);
}
}
);
DeviceEventEmitter.addListener(
'AudioPlaybackStopEvent', (evt) => {
if('playerId' in evt && instanceMap[evt.playerId] !== undefined){
instanceMap[evt.playerId].dispatchAudioPlaybackStopEvent(evt);
}
}
);
}
class ReactNativeStreamingPlayer extends EventEmitter {
constructor(soundUrl){
super();
this._nativeInstanceId = NATIVE_INSTANCE_COUNTER++;
this._currentSoundUrl = soundUrl;
ReactNativeAudioStreaming.createPlayer(this._nativeInstanceId);
instanceMap[this._nativeInstanceId] = this;
}
dispatchAudioEvent(evt){
this.trigger('stateChange',evt);
}
dispatchAudioPlaybackStopEvent(evt){
this.trigger('playbackStopped',evt);
}
play(){
if( this._currentSoundUrl.indexOf('://') > -1 ){
ReactNativeAudioStreaming.playWithKey(this._nativeInstanceId,this._currentSoundUrl);
} else {
ReactNativeAudioStreaming.playLocalWithKey(this._nativeInstanceId,this._currentSoundUrl);
}
}
pause(){
ReactNativeAudioStreaming.pauseWithKey(this._nativeInstanceId);
}
resume(){
ReactNativeAudioStreaming.resumeWithKey(this._nativeInstanceId);
}
stop(){
ReactNativeAudioStreaming.stopWithKey(this._nativeInstanceId);
}
setVolume(volInt){
ReactNativeAudioStreaming.setVolumeWithKey(this._nativeInstanceId,volInt);
}
setPan(panInt){
panInt = parseInt(panInt);
if(panInt < -1 || panInt > 1){
throw new Error("Out of range pan value provided");
}
ReactNativeAudioStreaming.setPanWithKey(this._nativeInstanceId,panInt);
}
seekToTime(secondsDouble){
ReactNativeAudioStreaming.seekToTimeWithKey(this._nativeInstanceId,secondsDouble);
}
goForwardWithKey(secondsDouble){
ReactNativeAudioStreaming.goForwardWithKey(this._nativeInstanceId,secondsDouble);
}
goBackWithKey(secondsDouble){
ReactNativeAudioStreaming.goBackWithKey(this._nativeInstanceId,secondsDouble);
}
setSoundUrl(urlString){
this._currentSoundUrl = urlString;
}
getSoundUrl(){
return this._currentSoundUrl;
}
getPan(cb){ //@TODO: implement objective-c bridged methods for getters
return ReactNativeAudioStreaming.getPanWithKey(this._nativeInstanceId,(err,data) => {
cb(err,data.pan);
});
}
getVolume(cb){
return ReactNativeAudioStreaming.getVolumeWithKey(this._nativeInstanceId,(err,data) => {
cb(err,data.volume);
});
}
isPaused (cb){
ReactNativeAudioStreaming.getStatusWithKey(this._nativeInstanceId,(err,data) => {
cb(err,data.status == "PAUSED");
});
}
isPlaying (cb){
ReactNativeAudioStreaming.getStatusWithKey(this._nativeInstanceId,(err,data) => {
console.log('callback for getStatus called')
cb(err,data.status == "PLAYING");
});
}
getPosition(cb){
ReactNativeAudioStreaming.getStatusWithKey(this._nativeInstanceId,(err,data) => {
console.log('callback for getStatus called')
cb(err,data.progress,data.duration);
});
}
getStatus(cb){
ReactNativeAudioStreaming.getStatusWithKey(this._nativeInstanceId,cb);
}
destroy(){
ReactNativeAudioStreaming.destroyWithKey(this._nativeInstanceId);
instanceMap[this._nativeInstanceId] = undefined;
}
setNowPlayingInfo(trackName,imageName){
ReactNativeAudioStreaming.setNowPlayingInfo(trackName,imageName);
}
}
subscribeGlobalAudioEvents();
export default ReactNativeStreamingPlayer;