tooot/src/components/Timelines/Timeline/Shared/Attachment/Audio.tsx

134 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-25 21:09:43 +01:00
import React, { useCallback, useState } from 'react'
import { Image, Pressable, StyleSheet, View } from 'react-native'
import { Audio } from 'expo-av'
2020-12-26 23:05:17 +01:00
import Button from '@components/Button'
2020-12-25 21:09:43 +01:00
import { Surface } from 'gl-react-expo'
import { Blurhash } from 'gl-react-blurhash'
import Slider from '@react-native-community/slider'
import { StyleConstants } from '@root/utils/styles/constants'
import { useTheme } from '@root/utils/styles/ThemeManager'
export interface Props {
sensitiveShown: boolean
audio: Mastodon.AttachmentAudio
}
const AttachmentAudio: React.FC<Props> = ({ sensitiveShown, audio }) => {
const { theme } = useTheme()
const [audioPlayer, setAudioPlayer] = useState<Audio.Sound>()
const [audioPlaying, setAudioPlaying] = useState(false)
const [audioPosition, setAudioPosition] = useState(0)
const playAudio = useCallback(async () => {
if (!audioPlayer) {
2020-12-26 23:05:17 +01:00
await Audio.setAudioModeAsync({
playsInSilentModeIOS: true,
interruptionModeIOS: 1
})
2020-12-25 21:09:43 +01:00
const { sound } = await Audio.Sound.createAsync(
{ uri: audio.url },
{},
// @ts-ignore
props => setAudioPosition(props.positionMillis)
)
setAudioPlayer(sound)
} else {
await audioPlayer.setPositionAsync(audioPosition)
audioPlayer.playAsync()
setAudioPlaying(true)
}
}, [audioPlayer, audioPosition])
const pauseAudio = useCallback(async () => {
audioPlayer!.pauseAsync()
setAudioPlaying(false)
}, [audioPlayer])
return (
2020-12-28 16:54:19 +01:00
<View style={styles.base}>
2020-12-26 23:05:17 +01:00
<View style={styles.overlay}>
2020-12-25 21:09:43 +01:00
{sensitiveShown ? (
audio.blurhash && (
<Surface
style={{
width: '100%',
height: '100%'
}}
>
<Blurhash hash={audio.blurhash} />
</Surface>
)
) : (
<>
{(audio.preview_url || audio.preview_remote_url) && (
<Image
style={styles.background}
source={{ uri: audio.preview_url || audio.preview_remote_url }}
/>
)}
2020-12-26 23:05:17 +01:00
<Button
type='icon'
2020-12-28 16:54:19 +01:00
content={audioPlaying ? 'pause-circle' : 'play-circle'}
2020-12-25 21:09:43 +01:00
size='L'
2020-12-28 16:54:19 +01:00
round
2020-12-26 23:05:17 +01:00
overlay
2020-12-25 21:09:43 +01:00
{...(audioPlaying
? { onPress: pauseAudio }
: { onPress: playAudio })}
/>
</>
)}
2020-12-26 23:05:17 +01:00
</View>
2020-12-25 21:09:43 +01:00
<View
style={{
2020-12-28 16:54:19 +01:00
position: 'absolute',
bottom: 0,
width: '100%',
2020-12-25 21:09:43 +01:00
backgroundColor: theme.backgroundOverlay,
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding,
paddingVertical: StyleConstants.Spacing.XS,
borderRadius: 6,
opacity: sensitiveShown ? 0.35 : undefined
}}
>
<Slider
style={{
width: '100%'
}}
minimumValue={0}
maximumValue={audio.meta.original.duration * 1000}
value={audioPosition}
2020-12-26 14:40:10 +01:00
minimumTrackTintColor={theme.secondary}
maximumTrackTintColor={theme.disabled}
2020-12-25 21:09:43 +01:00
onSlidingStart={() => {
audioPlayer?.pauseAsync()
setAudioPlaying(false)
}}
onSlidingComplete={value => {
setAudioPosition(value)
}}
/>
</View>
2020-12-28 16:54:19 +01:00
</View>
2020-12-25 21:09:43 +01:00
)
}
const styles = StyleSheet.create({
2020-12-28 16:54:19 +01:00
base: {
flex: 1,
flexBasis: '50%',
aspectRatio: 16 / 9,
padding: StyleConstants.Spacing.XS / 2
},
2020-12-25 21:09:43 +01:00
background: { position: 'absolute', width: '100%', height: '100%' },
overlay: {
position: 'absolute',
width: '100%',
height: '100%',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}
})
export default AttachmentAudio