2017-12-02 13:19:58 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-03-01 16:49:11 +00:00
|
|
|
View, StyleSheet, Text, Easing
|
2018-09-25 19:28:42 +00:00
|
|
|
} 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';
|
2018-09-19 14:18:32 +00:00
|
|
|
import { BorderlessButton } from 'react-native-gesture-handler';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2018-09-11 16:32:52 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
import Markdown from './Markdown';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
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: {
|
2019-03-01 16:49:11 +00:00
|
|
|
color: '#1D74F5'
|
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
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
export default class Audio extends React.Component {
|
2017-12-02 13:19:58 +00:00
|
|
|
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 }`
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
|
|
|
currentTime, duration, paused, uri
|
|
|
|
} = this.state;
|
|
|
|
const { file } = this.props;
|
|
|
|
if (nextState.currentTime !== currentTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.duration !== duration) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.paused !== paused) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.uri !== uri) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextProps.file, file)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
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-25 19:28:42 +00:00
|
|
|
const { duration } = this.state;
|
|
|
|
if (data.currentTime <= 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() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { duration } = this.state;
|
|
|
|
return formatTime(duration);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
togglePlayPause() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { paused } = this.state;
|
|
|
|
this.setState({ paused: !paused });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
|
|
|
uri, paused, currentTime, duration
|
|
|
|
} = this.state;
|
2018-09-11 16:32:52 +00:00
|
|
|
const {
|
|
|
|
user, baseUrl, customEmojis, file
|
|
|
|
} = this.props;
|
|
|
|
const { description } = file;
|
2018-09-14 19:39:52 +00:00
|
|
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
/>
|
2018-09-19 14:18:32 +00:00
|
|
|
<BorderlessButton
|
2017-12-02 13:19:58 +00:00
|
|
|
style={styles.playPauseButton}
|
|
|
|
onPress={() => this.togglePlayPause()}
|
|
|
|
>
|
|
|
|
{
|
2018-09-25 19:28:42 +00:00
|
|
|
paused
|
2019-03-01 16:49:11 +00:00
|
|
|
? <CustomIcon name='play' size={30} style={styles.playPauseImage} />
|
|
|
|
: <CustomIcon name='pause' size={30} style={styles.playPauseImage} />
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2018-09-19 14:18:32 +00:00
|
|
|
</BorderlessButton>
|
2018-09-11 16:32:52 +00:00
|
|
|
<Slider
|
|
|
|
style={styles.slider}
|
2018-09-25 19:28:42 +00:00
|
|
|
value={currentTime}
|
|
|
|
maximumValue={duration}
|
2018-09-11 16:32:52 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|