-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
171 lines (153 loc) · 6.85 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
import React from 'react'
import { View, Image, Text, Slider, TouchableOpacity, Platform, Alert} from 'react-native';
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{
static navigationOptions = props => ({
title:props.navigation.state.params.title,
})
constructor(){
super();
this.state = {
playState:'paused', //playing, paused
playSeconds:0,
duration:0
}
this.sliderEditing = false;
}
componentDidMount(){
this.play();
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});
}
}
play = async () => {
if(this.sound){
this.sound.play(this.playComplete);
this.setState({playState:'playing'});
}else{
const filepath = this.props.navigation.state.params.filepath;
var dirpath = '';
if (this.props.navigation.state.params.dirpath) {
dirpath = this.props.navigation.state.params.dirpath;
}
console.log('[Play]', filepath);
this.sound = new Sound(filepath, dirpath, (error) => {
if (error) {
console.log('failed to load the sound', error);
Alert.alert('Notice', 'audio file error. (Error code : 1)');
this.setState({playState:'paused'});
}else{
this.setState({playState:'playing', duration:this.sound.getDuration()});
this.sound.play(this.playComplete);
}
});
}
}
playComplete = (success) => {
if(this.sound){
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
Alert.alert('Notice', 'audio file error. (Error code : 2)');
}
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);
return (
<View style={{flex:1, justifyContent:'center', backgroundColor:'black'}}>
<Image source={img_speaker} style={{width:150, height:150, marginBottom:15, alignSelf:'center'}}/>
<View style={{flexDirection:'row', justifyContent:'center', marginVertical:15}}>
<TouchableOpacity onPress={this.jumpPrev15Seconds} style={{justifyContent:'center'}}>
<Image source={img_playjumpleft} style={{width:30, height:30}}/>
<Text style={{position:'absolute', alignSelf:'center', marginTop:1, color:'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}}/>
</TouchableOpacity>}
{this.state.playState == 'paused' &&
<TouchableOpacity onPress={this.play} style={{marginHorizontal:20}}>
<Image source={img_play} style={{width:30, height:30}}/>
</TouchableOpacity>}
<TouchableOpacity onPress={this.jumpNext15Seconds} style={{justifyContent:'center'}}>
<Image source={img_playjumpright} style={{width:30, height:30}}/>
<Text style={{position:'absolute', alignSelf:'center', marginTop:1, color:'white', fontSize:12}}>15</Text>
</TouchableOpacity>
</View>
<View style={{marginVertical:15, marginHorizontal:15, flexDirection:'row'}}>
<Text style={{color:'white', alignSelf:'center'}}>{currentTimeString}</Text>
<Slider
onTouchStart={this.onSliderEditStart}
// onTouchMove={() => console.log('onTouchMove')}
onTouchEnd={this.onSliderEditEnd}
// onTouchEndCapture={() => console.log('onTouchEndCapture')}
// onTouchCancel={() => console.log('onTouchCancel')}
onValueChange={this.onSliderEditing}
value={this.state.playSeconds} maximumValue={this.state.duration} maximumTrackTintColor='gray' minimumTrackTintColor='white' thumbTintColor='white'
style={{flex:1, alignSelf:'center', marginHorizontal:Platform.select({ios:5})}}/>
<Text style={{color:'white', alignSelf:'center'}}>{durationString}</Text>
</View>
</View>
)
}
}