-
Notifications
You must be signed in to change notification settings - Fork 103
/
main.js
196 lines (178 loc) · 5.08 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, {Component} from 'react';
import {StyleSheet, Text, TouchableOpacity, View, ScrollView, Alert} from 'react-native';
import Sound from 'react-native-sound';
const styles = StyleSheet.create({
container: {
flex: 1,
},
scrollContainer: {},
title: {
fontSize: 20,
fontWeight: 'bold',
paddingTop: 30,
padding: 20,
textAlign: 'center',
backgroundColor: 'rgba(240,240,240,1)',
},
button: {
fontSize: 20,
backgroundColor: 'rgba(220,220,220,1)',
borderRadius: 4,
borderWidth: 1,
borderColor: 'rgba(80,80,80,0.5)',
overflow: 'hidden',
padding: 7,
},
header: {
textAlign: 'left',
},
feature: {
flexDirection: 'row',
padding: 10,
alignSelf: 'stretch',
alignItems: 'center',
borderTopWidth: 1,
borderTopColor: 'rgb(180,180,180)',
borderBottomWidth: 1,
borderBottomColor: 'rgb(230,230,230)',
},
});
const Button = ({title, onPress}) => (
<TouchableOpacity onPress={onPress}>
<Text style={styles.button}>{title}</Text>
</TouchableOpacity>
);
const Header = ({children, style}) => <Text style={[styles.header, style]}>{children}</Text>;
const Feature = ({title, onPress, buttonLabel = 'PLAY', status}) => (
<View style={styles.feature}>
<Header style={{flex: 1}}>{title}</Header>
{status ? <Text style={{padding: 5}}>{resultIcons[status] || ''}</Text> : null}
<Button title={buttonLabel} onPress={onPress} />
</View>
);
const resultIcons = {
'': '',
pending: '?',
playing: '\u25B6',
win: '\u2713',
fail: '\u274C',
};
const audioTests = [
{
title: 'mp3 in bundle',
url: 'advertising.mp3',
basePath: Sound.MAIN_BUNDLE,
},
{
title: 'mp3 in bundle (looped)',
url: 'advertising.mp3',
basePath: Sound.MAIN_BUNDLE,
onPrepared: (sound, component) => {
sound.setNumberOfLoops(-1);
component.setState({loopingSound: sound});
},
},
{
title: 'mp3 via require()',
isRequire: true,
url: require('./advertising.mp3'),
},
{
title: 'mp3 remote download',
url: 'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/advertising.mp3',
},
{
title: 'mp3 remote - file doesn\'t exist',
url: 'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/file-not-here.mp3',
},
{
title: 'aac remote download',
url: 'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/pew2.aac',
},
{
title: 'wav remote download',
url: 'https://raw.githubusercontent.com/zmxv/react-native-sound-demo/master/frog.wav',
},
{
title: 'aac via require()',
isRequire: true,
url: require('./pew2.aac'),
},
{
title: 'wav via require()',
isRequire: true,
url: require('./frog.wav'),
},
];
function setTestState(testInfo, component, status) {
component.setState({tests: {...component.state.tests, [testInfo.title]: status}});
}
/**
* Generic play function for majority of tests
*/
function playSound(testInfo, component) {
setTestState(testInfo, component, 'pending');
const callback = (error, sound) => {
if (error) {
Alert.alert('error', error.message);
setTestState(testInfo, component, 'fail');
return;
}
setTestState(testInfo, component, 'playing');
// Run optional pre-play callback
testInfo.onPrepared && testInfo.onPrepared(sound, component);
sound.play(() => {
// Success counts as getting to the end
setTestState(testInfo, component, 'win');
// Release when it's done so we're not using up resources
sound.release();
});
};
// If the audio is a 'require' then the second parameter must be the callback.
if (testInfo.isRequire) {
const sound = new Sound(testInfo.url, error => callback(error, sound));
} else {
const sound = new Sound(testInfo.url, testInfo.basePath, error => callback(error, sound));
}
}
class MainView extends Component {
constructor(props) {
super(props);
Sound.setCategory('Playback', true); // true = mixWithOthers
// Special case for stopping
this.stopSoundLooped = () => {
if (!this.state.loopingSound) {
return;
}
this.state.loopingSound.stop().release();
this.setState({loopingSound: null, tests: {...this.state.tests, ['mp3 in bundle (looped)']: 'win'}});
};
this.state = {
loopingSound: undefined,
tests: {},
};
}
render() {
return (
<View style={styles.container}>
<Header style={styles.title}>react-native-sound-demo</Header>
<ScrollView style={styles.container} contentContainerStyle={styles.scrollContainer}>
{audioTests.map(testInfo => {
return (
<Feature
status={this.state.tests[testInfo.title]}
key={testInfo.title}
title={testInfo.title}
onPress={() => {
return playSound(testInfo, this);
}}
/>
);
})}
<Feature title="mp3 in bundle (looped)" buttonLabel={'STOP'} onPress={this.stopSoundLooped} />
</ScrollView>
</View>
);
}
}
export default MainView;