2023-08-02 19:50:20 +00:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from 'react';
|
|
|
|
import { StyleProp, StyleSheet, Text, TextStyle, View, useWindowDimensions } from 'react-native';
|
2022-08-08 21:02:08 +00:00
|
|
|
import { Audio, AVPlaybackStatus, InterruptionModeAndroid, InterruptionModeIOS } 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';
|
2023-08-04 21:39:23 +00:00
|
|
|
import { activateKeepAwakeAsync, deactivateKeepAwake } from 'expo-keep-awake';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { Sound } from 'expo-av/build/Audio/Sound';
|
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';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2019-03-29 19:36:07 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2023-05-11 23:20:48 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isAndroid, isIOS } from '../../lib/methods/helpers';
|
2020-04-30 20:05:59 +00:00
|
|
|
import MessageContext from './Context';
|
2020-04-30 18:54:27 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2023-05-11 19:06:50 +00:00
|
|
|
import { IAttachment, IUserMessage } from '../../definitions';
|
2023-08-02 19:50:20 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2023-07-03 17:28:22 +00:00
|
|
|
import { downloadMediaFile, getMediaCache } from '../../lib/methods/handleMediaDownload';
|
2022-11-16 13:40:45 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
|
|
|
import { PAUSE_AUDIO } from './constants';
|
2023-06-07 15:09:43 +00:00
|
|
|
import { fetchAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IButton {
|
|
|
|
loading: boolean;
|
|
|
|
paused: boolean;
|
2022-03-21 20:44:06 +00:00
|
|
|
disabled?: boolean;
|
2022-04-01 21:52:38 +00:00
|
|
|
onPress: () => void;
|
2023-06-07 15:09:43 +00:00
|
|
|
cached: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IMessageAudioProps {
|
2022-03-21 20:44:06 +00:00
|
|
|
file: IAttachment;
|
|
|
|
isReply?: boolean;
|
|
|
|
style?: StyleProp<TextStyle>[];
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2023-05-11 19:06:50 +00:00
|
|
|
author?: IUserMessage;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:54:27 +00:00
|
|
|
const mode = {
|
|
|
|
allowsRecordingIOS: false,
|
|
|
|
playsInSilentModeIOS: true,
|
2021-09-21 14:47:00 +00:00
|
|
|
staysActiveInBackground: true,
|
2020-04-30 18:54:27 +00:00
|
|
|
shouldDuckAndroid: true,
|
|
|
|
playThroughEarpieceAndroid: false,
|
2022-08-08 21:02:08 +00:00
|
|
|
interruptionModeIOS: InterruptionModeIOS.DoNotMix,
|
|
|
|
interruptionModeAndroid: InterruptionModeAndroid.DoNotMix
|
2020-04-30 18:54:27 +00:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const formatTime = (seconds: number) => moment.utc(seconds * 1000).format('mm:ss');
|
|
|
|
|
|
|
|
const BUTTON_HIT_SLOP = { top: 12, right: 12, bottom: 12, left: 12 };
|
|
|
|
|
2023-06-07 15:09:43 +00:00
|
|
|
const Button = React.memo(({ loading, paused, onPress, disabled, cached }: IButton) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
let customIconName: 'arrow-down-circle' | 'play-filled' | 'pause-filled' = 'arrow-down-circle';
|
|
|
|
if (cached) {
|
|
|
|
customIconName = paused ? 'play-filled' : 'pause-filled';
|
|
|
|
}
|
2023-05-11 18:52:48 +00:00
|
|
|
return (
|
|
|
|
<Touchable
|
|
|
|
style={styles.playPauseButton}
|
|
|
|
disabled={disabled}
|
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
2023-06-07 15:09:43 +00:00
|
|
|
background={Touchable.SelectableBackgroundBorderless()}
|
|
|
|
>
|
2023-05-11 18:52:48 +00:00
|
|
|
{loading ? (
|
|
|
|
<ActivityIndicator style={[styles.playPauseButton, styles.audioLoading]} />
|
|
|
|
) : (
|
2023-06-07 15:09:43 +00:00
|
|
|
<CustomIcon name={customIconName} size={36} color={disabled ? colors.tintDisabled : colors.tintColor} />
|
2023-05-11 18:52:48 +00:00
|
|
|
)}
|
|
|
|
</Touchable>
|
|
|
|
);
|
|
|
|
});
|
2019-05-20 20:43:50 +00:00
|
|
|
|
|
|
|
Button.displayName = 'MessageAudioButton';
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const MessageAudio = ({ file, getCustomEmoji, author, isReply, style }: IMessageAudioProps) => {
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [currentTime, setCurrentTime] = useState(0);
|
|
|
|
const [duration, setDuration] = useState(0);
|
|
|
|
const [paused, setPaused] = useState(true);
|
|
|
|
const [cached, setCached] = useState(false);
|
2022-11-16 13:40:45 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
|
|
|
const { scale } = useWindowDimensions();
|
|
|
|
const { theme } = useTheme();
|
2023-05-11 18:52:48 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const sound = useRef<Sound | null>(null);
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const pauseSound = () => {
|
|
|
|
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
|
|
|
|
togglePlayPause();
|
|
|
|
};
|
2020-05-08 12:55:26 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
|
|
|
|
if (status) {
|
|
|
|
onLoad(status);
|
|
|
|
onProgress(status);
|
|
|
|
onEnd(status);
|
2020-04-30 18:54:27 +00:00
|
|
|
}
|
2023-08-02 19:50:20 +00:00
|
|
|
};
|
2023-07-03 17:28:22 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const getUrl = () => {
|
2023-07-03 17:28:22 +00:00
|
|
|
let url = file.audio_url;
|
|
|
|
if (url && !url.startsWith('http')) {
|
|
|
|
url = `${baseUrl}${file.audio_url}`;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const onLoad = (data: AVPlaybackStatus) => {
|
2022-04-01 21:52:38 +00:00
|
|
|
if (data.isLoaded && data.durationMillis) {
|
|
|
|
const duration = data.durationMillis / 1000;
|
2023-08-02 19:50:20 +00:00
|
|
|
setDuration(duration > 0 ? duration : 0);
|
2022-04-01 21:52:38 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const onProgress = (data: AVPlaybackStatus) => {
|
2022-04-01 21:52:38 +00:00
|
|
|
if (data.isLoaded) {
|
|
|
|
const currentTime = data.positionMillis / 1000;
|
|
|
|
if (currentTime <= duration) {
|
2023-08-02 19:50:20 +00:00
|
|
|
setCurrentTime(currentTime);
|
2022-04-01 21:52:38 +00:00
|
|
|
}
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const onEnd = async (data: AVPlaybackStatus) => {
|
2022-04-01 21:52:38 +00:00
|
|
|
if (data.isLoaded) {
|
|
|
|
if (data.didJustFinish) {
|
|
|
|
try {
|
2023-08-02 19:50:20 +00:00
|
|
|
await sound.current?.stopAsync();
|
|
|
|
setPaused(true);
|
|
|
|
setCurrentTime(0);
|
|
|
|
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
|
2022-04-01 21:52:38 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
2020-04-30 18:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const getDuration = () => formatTime(currentTime || duration);
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const togglePlayPause = () => {
|
|
|
|
setPaused(!paused);
|
2023-08-04 21:39:23 +00:00
|
|
|
playPause(!paused);
|
|
|
|
};
|
|
|
|
|
|
|
|
const playPause = async (isPaused: boolean) => {
|
|
|
|
try {
|
|
|
|
if (isPaused) {
|
|
|
|
await sound.current?.pauseAsync();
|
|
|
|
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
|
|
|
|
} else {
|
|
|
|
EventEmitter.emit(PAUSE_AUDIO);
|
|
|
|
EventEmitter.addEventListener(PAUSE_AUDIO, pauseSound);
|
|
|
|
await Audio.setAudioModeAsync(mode);
|
|
|
|
await sound.current?.playAsync();
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const handleDownload = async () => {
|
|
|
|
setLoading(true);
|
2023-05-19 14:35:36 +00:00
|
|
|
try {
|
2023-08-02 19:50:20 +00:00
|
|
|
const url = getUrl();
|
2023-07-04 17:22:20 +00:00
|
|
|
if (url) {
|
2023-05-19 14:35:36 +00:00
|
|
|
const audio = await downloadMediaFile({
|
|
|
|
downloadUrl: `${url}?rc_uid=${user.id}&rc_token=${user.token}`,
|
2023-07-04 17:22:20 +00:00
|
|
|
type: 'audio',
|
|
|
|
mimeType: file.audio_type
|
2023-05-19 14:35:36 +00:00
|
|
|
});
|
2023-08-02 19:50:20 +00:00
|
|
|
await sound.current?.loadAsync({ uri: audio });
|
|
|
|
setLoading(false);
|
|
|
|
setCached(true);
|
2023-05-19 05:42:21 +00:00
|
|
|
}
|
2023-05-19 14:35:36 +00:00
|
|
|
} catch {
|
2023-08-02 19:50:20 +00:00
|
|
|
setLoading(false);
|
|
|
|
setCached(false);
|
2023-07-03 17:28:22 +00:00
|
|
|
}
|
2023-05-11 18:52:48 +00:00
|
|
|
};
|
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const handleAutoDownload = async () => {
|
|
|
|
const url = getUrl();
|
2020-04-30 18:54:27 +00:00
|
|
|
try {
|
2023-08-02 19:50:20 +00:00
|
|
|
if (url) {
|
|
|
|
const isCurrentUserAuthor = author?._id === user.id;
|
|
|
|
const isAutoDownloadEnabled = fetchAutoDownloadEnabled('audioPreferenceDownload');
|
|
|
|
if (isAutoDownloadEnabled || isCurrentUserAuthor) {
|
|
|
|
await handleDownload();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
setCached(false);
|
2020-04-30 18:54:27 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-30 18:54:27 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
const onPress = () => {
|
|
|
|
if (cached) {
|
|
|
|
togglePlayPause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleDownload();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onValueChange = async (value: number) => {
|
2020-04-30 18:54:27 +00:00
|
|
|
try {
|
2023-08-02 19:50:20 +00:00
|
|
|
setCurrentTime(value);
|
|
|
|
await sound.current?.setPositionAsync(value * 1000);
|
2020-04-30 18:54:27 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-05-20 20:43:50 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
sound.current = new Audio.Sound();
|
|
|
|
sound.current?.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
|
|
|
|
|
|
|
|
const handleCache = async () => {
|
|
|
|
const cachedAudioResult = await getMediaCache({
|
|
|
|
type: 'audio',
|
|
|
|
mimeType: file.audio_type,
|
|
|
|
urlToCache: getUrl()
|
|
|
|
});
|
|
|
|
if (cachedAudioResult?.exists) {
|
|
|
|
await sound.current?.loadAsync({ uri: cachedAudioResult.uri });
|
|
|
|
setLoading(false);
|
|
|
|
setCached(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isReply) {
|
|
|
|
setLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await handleAutoDownload();
|
|
|
|
};
|
|
|
|
handleCache();
|
|
|
|
|
2023-08-04 21:39:23 +00:00
|
|
|
return () => {
|
|
|
|
EventEmitter.removeListener(PAUSE_AUDIO, pauseSound);
|
2023-08-02 19:50:20 +00:00
|
|
|
try {
|
2023-08-04 21:39:23 +00:00
|
|
|
sound.current?.stopAsync();
|
2023-08-02 19:50:20 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
};
|
2023-08-04 21:39:23 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-02 19:50:20 +00:00
|
|
|
if (paused) {
|
|
|
|
deactivateKeepAwake();
|
|
|
|
} else {
|
2023-08-04 21:39:23 +00:00
|
|
|
activateKeepAwakeAsync();
|
2018-09-14 19:39:52 +00:00
|
|
|
}
|
2023-08-02 19:50:20 +00:00
|
|
|
}, [paused]);
|
2018-09-14 19:39:52 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-12 16:27:05 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
let thumbColor;
|
|
|
|
if (isAndroid && isReply) {
|
|
|
|
thumbColor = themes[theme].tintDisabled;
|
|
|
|
} else if (isAndroid) {
|
|
|
|
thumbColor = themes[theme].tintColor;
|
2017-12-02 13:19:58 +00:00
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2023-08-02 19:50:20 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Markdown
|
|
|
|
msg={file.description}
|
|
|
|
style={[isReply && style]}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.audioContainer,
|
|
|
|
{ backgroundColor: themes[theme].chatComponentBackground, borderColor: themes[theme].borderColor }
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Button disabled={isReply} loading={loading} paused={paused} cached={cached} onPress={onPress} />
|
|
|
|
<Slider
|
|
|
|
disabled={isReply}
|
|
|
|
style={styles.slider}
|
|
|
|
value={currentTime}
|
|
|
|
maximumValue={duration}
|
|
|
|
minimumValue={0}
|
|
|
|
thumbTintColor={thumbColor}
|
|
|
|
minimumTrackTintColor={themes[theme].tintColor}
|
|
|
|
maximumTrackTintColor={themes[theme].auxiliaryText}
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
thumbImage={isIOS ? { uri: 'audio_thumb', scale } : undefined}
|
|
|
|
/>
|
|
|
|
<Text style={[styles.duration, { color: themes[theme].auxiliaryText }]}>{getDuration()}</Text>
|
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MessageAudio;
|