-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundApp.js
76 lines (70 loc) · 2.16 KB
/
SoundApp.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
// https://github.com/zmxv/react-native-sound
'use strict';
import React, {
Component,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
var Sound = require('react-native-sound');
var RNFS = require('react-native-fs');
export default class MainView extends Component {
render() {
return <View style={styles.container}>
<TouchableOpacity onPress={this.playSound}>
<Text style={styles.button}>hello</Text>
</TouchableOpacity>
</View>;
}
playSound() {
// todo: fetch html
// https://github.com/fritx/51voa-cli
const url = 'http://www.51voa.com/VOA_Standard_English/from-golf-range-to-free-range-69274.html'
console.log('fetching...', url)
fetch(url)
.then((response) => response.text())
.then((html) => {
// const url = 'http://downdb.51voa.com/201604/fusion-reactor-still-in-works.mp3'
// const file = RNFS.DocumentDirectoryPath + '/fusion.mp3'
const part = html.match(/Player\("(.+?)"\);/)[1]
const url = `http://downdb.51voa.com${part}`
const file = RNFS.DocumentDirectoryPath + '/golf.mp3'
console.log(url)
console.log(' => ', file)
RNFS.downloadFile(url, file, (d) => {
console.log('downloading', d)
}, (d) => {
// console.log(`${d.bytesWritten} / ${d.contentLength}`)
})
.then(() => {
console.log('downloaded')
// fixme: https://github.com/zmxv/react-native-sound/issues/27
// var s = new Sound('advertising.mp3', Sound.MAIN_BUNDLE, (e) => {
// var s = new Sound(file + '.aa', Sound.MAIN_BUNDLE, (e) => {
var s = new Sound(file, '/', (e) => { // hack
if (e) {
console.log('error', e);
} else {
console.log('duration', s.getDuration());
s.play();
}
});
})
})
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
button: {
fontSize: 20,
backgroundColor: 'silver',
padding: 5,
},
});
export default MainView;