forked from benevbright/react-native-sound-playerview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (179 loc) · 7.58 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
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
197
198
import React from 'react'
import {Image, Platform, Text, TouchableOpacity, View} from 'react-native';
import Slider from '@react-native-community/slider';
import Sound from 'react-native-sound';
const img_speaker = require('./resources/ui_speaker.png');
const img_pause = require('./resources/ui_pause.png');
const img_play = require('./resources/ui_play.png');
const img_playjumpleft = require('./resources/ui_playjumpleft.png');
const img_playjumpright = require('./resources/ui_playjumpright.png');
export default class PlayerScreen extends React.Component {
constructor() {
super();
this.state = {
inited: false,
loaded: false,
playState: 'paused', //playing, paused
playSeconds: 0,
duration: 0
};
this.sliderEditing = false;
}
componentDidMount() {
this.initSound();
if (this.props.onLoadStart) {
this.props.onLoadStart();
}
this.timeout = setInterval(() => {
if (this.sound && this.sound.isLoaded() && this.state.playState === 'playing' && !this.sliderEditing) {
this.sound.getCurrentTime((seconds, isPlaying) => {
this.setState({playSeconds: seconds});
})
}
}, 100);
}
componentWillUnmount() {
if (this.sound) {
this.sound.release();
this.sound = null;
}
if (this.timeout) {
clearInterval(this.timeout);
}
}
onSliderEditStart = () => {
this.sliderEditing = true;
};
onSliderEditEnd = () => {
this.sliderEditing = false;
};
onSliderEditing = value => {
if (this.sound) {
this.sound.setCurrentTime(value);
this.setState({playSeconds: value});
}
};
initSound = () => {
const filepath = this.props.filepath;
if (__DEV__) console.log('[Play]', filepath);
this.sound = new Sound(filepath, '', (error) => {
if (error) {
if (this.props.onErrorLoad) {
this.props.onErrorLoad();
}
this.setState({playState: 'paused', inited: false});
} else {
this.sound.setCategory('Playback');
this.setState({inited: true, loaded: this.sound.isLoaded(), duration: this.sound.getDuration()});
if (this.props.onLoad) {
this.props.onLoad();
}
}
});
};
play = async () => {
if (this.sound) {
this.sound.play(this.playComplete);
this.setState({playState: 'playing'});
} else {
this.initSound();
}
};
playComplete = (success) => {
if (this.sound) {
if (success) {
if (__DEV__) console.log('successfully finished playing');
} else {
if (__DEV__) console.log('playback failed due to audio decoding errors');
if (this.props.onErrorComplete) {
this.props.onErrorComplete();
}
}
this.setState({playState: 'paused', playSeconds: 0});
this.sound.setCurrentTime(0);
}
};
pause = () => {
if (this.sound) {
this.sound.pause();
}
this.setState({playState: 'paused'});
};
jumpPrev15Seconds = () => {
this.jumpSeconds(-15);
};
jumpNext15Seconds = () => {
this.jumpSeconds(15);
};
jumpSeconds = (secsDelta) => {
if (this.sound) {
this.sound.getCurrentTime((secs, isPlaying) => {
let nextSecs = secs + secsDelta;
if (nextSecs < 0) nextSecs = 0;
else if (nextSecs > this.state.duration) nextSecs = this.state.duration;
this.sound.setCurrentTime(nextSecs);
this.setState({playSeconds: nextSecs});
})
}
};
getAudioTimeString(seconds) {
const h = parseInt(seconds / (60 * 60));
const m = parseInt(seconds % (60 * 60) / 60);
const s = parseInt(seconds % 60);
return ((h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s));
}
render() {
const currentTimeString = this.getAudioTimeString(this.state.playSeconds);
const durationString = this.getAudioTimeString(this.state.duration);
const {backgroundColor, tintColor, speakerColor} = this.props;
return (
<View style={{flex: 1, justifyContent: 'center', backgroundColor: backgroundColor || 'black'}}>
<Image source={img_speaker} style={{width: 150, height: 150, marginBottom: 15, alignSelf: 'center', tintColor: speakerColor || 'gray'}}/>
<View style={{flexDirection: 'row', justifyContent: 'center', marginVertical: 15}}>
<TouchableOpacity onPress={this.jumpPrev15Seconds} style={{justifyContent: 'center'}}>
<Image source={img_playjumpleft} style={{width: 30, height: 30, tintColor: tintColor || 'white' }}/>
<Text style={{
position: 'absolute',
alignSelf: 'center',
marginTop: 1,
color: tintColor || 'white',
fontSize: 12
}}>15</Text>
</TouchableOpacity>
{this.state.playState === 'playing' &&
<TouchableOpacity onPress={this.pause} style={{marginHorizontal: 20}}>
<Image source={img_pause} style={{width: 30, height: 30, tintColor: tintColor || 'white'}}/>
</TouchableOpacity>}
{this.state.playState === 'paused' &&
<TouchableOpacity onPress={this.play} style={{marginHorizontal: 20}}>
<Image source={img_play} style={{width: 30, height: 30, tintColor: tintColor || 'white'}}/>
</TouchableOpacity>}
<TouchableOpacity onPress={this.jumpNext15Seconds} style={{justifyContent: 'center'}}>
<Image source={img_playjumpright} style={{width: 30, height: 30, tintColor: tintColor || 'white'}}/>
<Text style={{
position: 'absolute',
alignSelf: 'center',
marginTop: 1,
color: tintColor || 'white',
fontSize: 12
}}>15</Text>
</TouchableOpacity>
</View>
<View style={{marginVertical: 15, marginHorizontal: 15, flexDirection: 'row'}}>
<Text style={{color: tintColor || 'white', alignSelf: 'center'}}>{currentTimeString}</Text>
<Slider
onTouchStart={this.onSliderEditStart}
onTouchEnd={this.onSliderEditEnd}
onValueChange={this.onSliderEditing}
value={this.state.playSeconds}
maximumValue={this.state.duration}
maximumTrackTintColor='gray'
minimumTrackTintColor={tintColor || 'white'}
thumbTintColor={tintColor || 'white'}
style={{flex: 1, alignSelf: 'center', marginHorizontal: Platform.select({ios: 5})}}/>
<Text style={{color: tintColor || 'white', alignSelf: 'center'}}>{durationString}</Text>
</View>
</View>
)
}
}