forked from tlenclos/react-native-audio-streaming
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
151 lines (140 loc) · 3.99 KB
/
index.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { Component } from 'react';
import {
NativeModules,
StyleSheet,
Text,
View,
TouchableOpacity,
DeviceEventEmitter,
ActivityIndicator,
Platform
} from 'react-native';
const { ReactNativeAudioStreaming } = NativeModules;
// Possibles states
const PLAYING = 'PLAYING';
const STREAMING = 'STREAMING';
const PAUSED = 'PAUSED';
const STOPPED = 'STOPPED';
const ERROR = 'ERROR';
const METADATA_UPDATED = 'METADATA_UPDATED';
const BUFFERING = 'BUFFERING';
const START_PREPARING = 'START_PREPARING'; // Android only
const BUFFERING_START = 'BUFFERING_START'; // Android only
// UI
const iconSize = 60;
class Player extends Component {
constructor(props) {
super(props);
this._onPress = this._onPress.bind(this);
this.state = {
status: STOPPED,
song: ''
};
}
componentDidMount() {
this.subscription = DeviceEventEmitter.addListener(
'AudioBridgeEvent', (evt) => {
// We just want meta update for song name
if (evt.status === METADATA_UPDATED && evt.key === 'StreamTitle') {
this.setState({song: evt.value});
} else if (evt.status != METADATA_UPDATED) {
this.setState(evt);
}
}
);
ReactNativeAudioStreaming.getStatus((error, status) => {
(error) ? console.log(error) : this.setState(status)
});
}
_onPress() {
switch (this.state.status) {
case PLAYING:
case STREAMING:
ReactNativeAudioStreaming.pause();
break;
case PAUSED:
ReactNativeAudioStreaming.resume();
break;
case STOPPED:
case ERROR:
ReactNativeAudioStreaming.play(this.props.url, {showIniOSMediaCenter: true, showInAndroidNotifications: true});
break;
case BUFFERING:
ReactNativeAudioStreaming.stop();
break;
}
}
render() {
let icon = null;
switch (this.state.status) {
case PLAYING:
case STREAMING:
icon = <Text style={styles.icon}>॥</Text>;
break;
case PAUSED:
case STOPPED:
case ERROR:
icon = <Text style={styles.icon}>▸</Text>;
break;
case BUFFERING:
case BUFFERING_START:
case START_PREPARING:
icon = <ActivityIndicator
animating={true}
style={{height: 80}}
size="large"
/>;
break;
}
return (
<TouchableOpacity style={styles.container} onPress={this._onPress}>
{icon}
<View style={styles.textContainer}>
<Text style={styles.songName}>{this.state.song}</Text>
</View>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
container: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
alignItems: 'center',
flexDirection: 'row',
height: 80,
paddingLeft: 10,
paddingRight: 10,
borderColor: '#000033',
borderTopWidth: 1,
},
icon: {
color: '#000',
fontSize: 26,
borderColor: '#000033',
borderWidth: 1,
borderRadius: iconSize / 2,
width: iconSize,
height: Platform.OS == 'ios' ? iconSize : 40,
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center',
paddingTop: Platform.OS == 'ios' ? 10 : 0
},
textContainer: {
flexDirection: 'column',
margin: 10
},
textLive: {
color: '#000',
marginBottom: 5
},
songName: {
fontSize: 20,
textAlign: 'center',
color: '#000'
}
});
export { Player, ReactNativeAudioStreaming }