2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-11 16:32:52 +00:00
|
|
|
import { View, StyleSheet, TouchableOpacity, Text, Easing, Image } from 'react-native';
|
2017-12-02 13:19:58 +00:00
|
|
|
import Video from 'react-native-video';
|
|
|
|
import Slider from 'react-native-slider';
|
2018-09-11 16:32:52 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
import Markdown from './Markdown';
|
2017-12-28 17:40:10 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
audioContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
2018-09-11 16:32:52 +00:00
|
|
|
height: 56,
|
|
|
|
backgroundColor: '#f7f8fa',
|
|
|
|
borderRadius: 4,
|
|
|
|
marginBottom: 10
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
playPauseButton: {
|
2018-09-11 16:32:52 +00:00
|
|
|
width: 56,
|
2017-12-02 13:19:58 +00:00
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: 'transparent'
|
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
playPauseImage: {
|
|
|
|
width: 30,
|
|
|
|
height: 30
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
slider: {
|
|
|
|
flex: 1,
|
|
|
|
marginRight: 10
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
duration: {
|
2018-09-11 16:32:52 +00:00
|
|
|
marginRight: 16,
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: '500',
|
|
|
|
color: '#54585e'
|
|
|
|
},
|
|
|
|
thumbStyle: {
|
|
|
|
width: 12,
|
|
|
|
height: 12
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
const formatTime = seconds => moment.utc(seconds * 1000).format('mm:ss');
|
2017-12-02 13:19:58 +00:00
|
|
|
|
|
|
|
export default class Audio extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
file: PropTypes.object.isRequired,
|
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-09-11 16:32:52 +00:00
|
|
|
user: PropTypes.object.isRequired,
|
|
|
|
customEmojis: PropTypes.object.isRequired
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.onLoad = this.onLoad.bind(this);
|
|
|
|
this.onProgress = this.onProgress.bind(this);
|
|
|
|
this.onEnd = this.onEnd.bind(this);
|
|
|
|
const { baseUrl, file, user } = props;
|
|
|
|
this.state = {
|
|
|
|
currentTime: 0,
|
|
|
|
duration: 0,
|
|
|
|
paused: true,
|
|
|
|
uri: `${ baseUrl }${ file.audio_url }?rc_uid=${ user.id }&rc_token=${ user.token }`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad(data) {
|
2018-03-07 00:17:20 +00:00
|
|
|
this.setState({ duration: data.duration > 0 ? data.duration : 0 });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onProgress(data) {
|
2018-09-11 16:32:52 +00:00
|
|
|
if (data.currentTime <= this.state.duration) {
|
2017-12-02 13:19:58 +00:00
|
|
|
this.setState({ currentTime: data.currentTime });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnd() {
|
|
|
|
this.setState({ paused: true, currentTime: 0 });
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.player.seek(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getDuration() {
|
|
|
|
return formatTime(this.state.duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePlayPause() {
|
|
|
|
this.setState({ paused: !this.state.paused });
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { uri, paused } = this.state;
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
|
|
|
user, baseUrl, customEmojis, file
|
|
|
|
} = this.props;
|
|
|
|
const { description } = file;
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
[
|
|
|
|
<View key='audio' style={styles.audioContainer}>
|
2017-12-02 13:19:58 +00:00
|
|
|
<Video
|
|
|
|
ref={(ref) => {
|
|
|
|
this.player = ref;
|
|
|
|
}}
|
|
|
|
source={{ uri }}
|
|
|
|
onLoad={this.onLoad}
|
|
|
|
onProgress={this.onProgress}
|
|
|
|
onEnd={this.onEnd}
|
|
|
|
paused={paused}
|
|
|
|
repeat={false}
|
|
|
|
/>
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.playPauseButton}
|
|
|
|
onPress={() => this.togglePlayPause()}
|
|
|
|
>
|
|
|
|
{
|
2018-09-11 16:32:52 +00:00
|
|
|
paused ?
|
|
|
|
<Image source={{ uri: 'play' }} style={styles.playPauseImage} /> :
|
|
|
|
<Image source={{ uri: 'pause' }} style={styles.playPauseImage} />
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
</TouchableOpacity>
|
2018-09-11 16:32:52 +00:00
|
|
|
<Slider
|
|
|
|
style={styles.slider}
|
|
|
|
value={this.state.currentTime}
|
|
|
|
maximumValue={this.state.duration}
|
|
|
|
minimumValue={0}
|
|
|
|
animateTransitions
|
|
|
|
animationConfig={{
|
|
|
|
duration: 250,
|
|
|
|
easing: Easing.linear,
|
|
|
|
delay: 0
|
|
|
|
}}
|
|
|
|
thumbTintColor='#1d74f5'
|
|
|
|
minimumTrackTintColor='#1d74f5'
|
|
|
|
onValueChange={value => this.setState({ currentTime: value })}
|
|
|
|
thumbStyle={styles.thumbStyle}
|
|
|
|
/>
|
|
|
|
<Text style={styles.duration}>{this.getDuration()}</Text>
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>,
|
2018-09-11 16:32:52 +00:00
|
|
|
<Markdown key='description' msg={description} baseUrl={baseUrl} customEmojis={customEmojis} username={user.username} />
|
2018-04-24 19:34:03 +00:00
|
|
|
]
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|