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 {
|
2020-06-17 17:35:58 +00:00
|
|
|
View, StyleSheet, Text, Easing
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2020-04-30 18:54:27 +00:00
|
|
|
import { Audio } from 'expo-av';
|
2019-10-04 13:38:35 +00:00
|
|
|
import Slider from '@react-native-community/slider';
|
2018-09-11 16:32:52 +00:00
|
|
|
import moment from 'moment';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2020-05-08 12:55:26 +00:00
|
|
|
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
|
2018-09-11 16:32:52 +00:00
|
|
|
|
2020-04-30 20:05:59 +00:00
|
|
|
import Touchable from './Touchable';
|
2019-08-27 12:25:38 +00:00
|
|
|
import Markdown from '../markdown';
|
2019-03-01 16:49:11 +00:00
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2019-10-04 13:38:35 +00:00
|
|
|
import { isAndroid, isIOS } from '../../utils/deviceInfo';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2020-04-30 18:54:27 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { withDimensions } from '../../dimensions';
|
2020-04-30 18:54:27 +00:00
|
|
|
|
|
|
|
const mode = {
|
|
|
|
allowsRecordingIOS: false,
|
|
|
|
playsInSilentModeIOS: true,
|
|
|
|
staysActiveInBackground: false,
|
|
|
|
shouldDuckAndroid: true,
|
|
|
|
playThroughEarpieceAndroid: false,
|
|
|
|
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
|
|
|
|
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX
|
|
|
|
};
|
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,
|
2019-03-29 19:36:07 +00:00
|
|
|
borderWidth: 1,
|
2018-09-11 16:32:52 +00:00
|
|
|
borderRadius: 4,
|
2019-03-29 19:36:07 +00:00
|
|
|
marginBottom: 6
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
playPauseButton: {
|
2019-04-24 18:36:29 +00:00
|
|
|
marginHorizontal: 10,
|
2017-12-02 13:19:58 +00:00
|
|
|
alignItems: 'center',
|
|
|
|
backgroundColor: 'transparent'
|
|
|
|
},
|
2020-04-30 18:54:27 +00:00
|
|
|
audioLoading: {
|
|
|
|
marginHorizontal: 8
|
|
|
|
},
|
2018-09-11 16:32:52 +00:00
|
|
|
slider: {
|
2019-04-24 18:36:29 +00:00
|
|
|
flex: 1
|
2017-12-02 13:19:58 +00:00
|
|
|
},
|
|
|
|
duration: {
|
2019-04-24 18:36:29 +00:00
|
|
|
marginHorizontal: 12,
|
2018-09-11 16:32:52 +00:00
|
|
|
fontSize: 14,
|
2019-03-29 19:36:07 +00:00
|
|
|
...sharedStyles.textRegular
|
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');
|
2019-04-24 18:36:29 +00:00
|
|
|
const BUTTON_HIT_SLOP = {
|
|
|
|
top: 12, right: 12, bottom: 12, left: 12
|
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
const sliderAnimationConfig = {
|
|
|
|
duration: 250,
|
|
|
|
easing: Easing.linear,
|
|
|
|
delay: 0
|
|
|
|
};
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
const Button = React.memo(({
|
|
|
|
loading, paused, onPress, theme
|
|
|
|
}) => (
|
2019-05-20 20:43:50 +00:00
|
|
|
<Touchable
|
|
|
|
style={styles.playPauseButton}
|
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
|
|
|
background={Touchable.SelectableBackgroundBorderless()}
|
|
|
|
>
|
2020-04-30 18:54:27 +00:00
|
|
|
{
|
|
|
|
loading
|
|
|
|
? <ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} theme={theme} />
|
2020-07-27 19:53:33 +00:00
|
|
|
: <CustomIcon name={paused ? 'play-filled' : 'pause-filled'} size={36} color={themes[theme].tintColor} />
|
2020-04-30 18:54:27 +00:00
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
</Touchable>
|
|
|
|
));
|
|
|
|
|
|
|
|
Button.propTypes = {
|
2020-04-30 18:54:27 +00:00
|
|
|
loading: PropTypes.bool,
|
2019-05-20 20:43:50 +00:00
|
|
|
paused: PropTypes.bool,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2019-05-20 20:43:50 +00:00
|
|
|
onPress: PropTypes.func
|
|
|
|
};
|
|
|
|
Button.displayName = 'MessageAudioButton';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
class MessageAudio extends React.Component {
|
2020-04-30 20:05:59 +00:00
|
|
|
static contextType = MessageContext;
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
static propTypes = {
|
|
|
|
file: PropTypes.object.isRequired,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2020-06-17 17:35:58 +00:00
|
|
|
getCustomEmoji: PropTypes.func,
|
|
|
|
scale: PropTypes.number
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-04-30 18:54:27 +00:00
|
|
|
loading: false,
|
2017-12-02 13:19:58 +00:00
|
|
|
currentTime: 0,
|
|
|
|
duration: 0,
|
2020-04-30 20:05:59 +00:00
|
|
|
paused: true
|
2017-12-02 13:19:58 +00:00
|
|
|
};
|
2020-04-30 18:54:27 +00:00
|
|
|
|
|
|
|
this.sound = new Audio.Sound();
|
|
|
|
this.sound.setOnPlaybackStatusUpdate(this.onPlaybackStatusUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-04-30 20:05:59 +00:00
|
|
|
const { file } = this.props;
|
|
|
|
const { baseUrl, user } = this.context;
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2020-05-08 17:07:58 +00:00
|
|
|
let url = file.audio_url;
|
|
|
|
if (!url.startsWith('http')) {
|
|
|
|
url = `${ baseUrl }${ file.audio_url }`;
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
this.setState({ loading: true });
|
|
|
|
try {
|
2020-05-08 17:07:58 +00:00
|
|
|
await this.sound.loadAsync({ uri: `${ url }?rc_uid=${ user.id }&rc_token=${ user.token }` });
|
2020-04-30 18:54:27 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
this.setState({ loading: false });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const {
|
2020-04-30 20:05:59 +00:00
|
|
|
currentTime, duration, paused, loading
|
2018-12-21 10:55:35 +00:00
|
|
|
} = this.state;
|
2020-06-15 14:00:46 +00:00
|
|
|
const { file, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.currentTime !== currentTime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.duration !== duration) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.paused !== paused) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextProps.file, file)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-30 18:54:27 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:55:26 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { paused } = this.state;
|
|
|
|
if (paused) {
|
|
|
|
deactivateKeepAwake();
|
|
|
|
} else {
|
|
|
|
activateKeepAwake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
async componentWillUnmount() {
|
|
|
|
try {
|
|
|
|
await this.sound.stopAsync();
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPlaybackStatusUpdate = (status) => {
|
|
|
|
if (status) {
|
|
|
|
this.onLoad(status);
|
|
|
|
this.onProgress(status);
|
|
|
|
this.onEnd(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onLoad = (data) => {
|
2020-04-30 18:54:27 +00:00
|
|
|
const duration = data.durationMillis / 1000;
|
|
|
|
this.setState({ duration: duration > 0 ? duration : 0 });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
onProgress = (data) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { duration } = this.state;
|
2020-04-30 18:54:27 +00:00
|
|
|
const currentTime = data.positionMillis / 1000;
|
|
|
|
if (currentTime <= duration) {
|
|
|
|
this.setState({ currentTime });
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
onEnd = async(data) => {
|
|
|
|
if (data.didJustFinish) {
|
|
|
|
try {
|
|
|
|
await this.sound.stopAsync();
|
|
|
|
this.setState({ paused: true, currentTime: 0 });
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:43:50 +00:00
|
|
|
get duration() {
|
2020-04-30 18:54:27 +00:00
|
|
|
const { currentTime, duration } = this.state;
|
|
|
|
return formatTime(currentTime || duration);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
togglePlayPause = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { paused } = this.state;
|
2020-04-30 18:54:27 +00:00
|
|
|
this.setState({ paused: !paused }, this.playPause);
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
playPause = async() => {
|
|
|
|
const { paused } = this.state;
|
|
|
|
try {
|
|
|
|
if (paused) {
|
|
|
|
await this.sound.pauseAsync();
|
|
|
|
} else {
|
2020-07-06 19:23:46 +00:00
|
|
|
await Audio.setAudioModeAsync(mode);
|
2020-04-30 18:54:27 +00:00
|
|
|
await this.sound.playAsync();
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onValueChange = async(value) => {
|
|
|
|
try {
|
|
|
|
this.setState({ currentTime: value });
|
|
|
|
await this.sound.setPositionAsync(value * 1000);
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
2020-04-30 18:54:27 +00:00
|
|
|
loading, paused, currentTime, duration
|
2018-09-25 19:28:42 +00:00
|
|
|
} = this.state;
|
2020-06-17 17:35:58 +00:00
|
|
|
const {
|
|
|
|
file, getCustomEmoji, theme, scale
|
|
|
|
} = this.props;
|
2018-09-11 16:32:52 +00:00
|
|
|
const { description } = file;
|
2020-04-30 20:05:59 +00:00
|
|
|
const { baseUrl, user } = this.context;
|
2018-09-14 19:39:52 +00:00
|
|
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.audioContainer,
|
2020-06-15 14:00:46 +00:00
|
|
|
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor }
|
2019-12-04 16:39:53 +00:00
|
|
|
]}
|
|
|
|
>
|
2020-04-30 18:54:27 +00:00
|
|
|
<Button loading={loading} paused={paused} onPress={this.togglePlayPause} theme={theme} />
|
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
|
2019-05-20 20:43:50 +00:00
|
|
|
animationConfig={sliderAnimationConfig}
|
2019-12-04 16:39:53 +00:00
|
|
|
thumbTintColor={isAndroid && themes[theme].tintColor}
|
|
|
|
minimumTrackTintColor={themes[theme].tintColor}
|
|
|
|
maximumTrackTintColor={themes[theme].auxiliaryText}
|
2019-05-20 20:43:50 +00:00
|
|
|
onValueChange={this.onValueChange}
|
2020-06-17 17:35:58 +00:00
|
|
|
thumbImage={isIOS && { uri: 'audio_thumb', scale }}
|
2018-09-11 16:32:52 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.duration, { color: themes[theme].auxiliaryText }]}>{this.duration}</Text>
|
2019-05-20 20:43:50 +00:00
|
|
|
</View>
|
2020-02-27 18:34:20 +00:00
|
|
|
<Markdown msg={description} baseUrl={baseUrl} username={user.username} getCustomEmoji={getCustomEmoji} theme={theme} />
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2017-12-02 13:19:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
export default withDimensions(MessageAudio);
|