feat: add a focus state to audio player (#5491)

* feat: add a focus state to audio player

* update the rcactivity indicator

---------

Co-authored-by: Gleidson Daniel Silva <gleidson10daniel@hotmail.com>
This commit is contained in:
Reinaldo Neto 2024-01-22 18:39:01 -03:00 committed by GitHub
parent dc0f824638
commit c2656459ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ const Icon = ({ audioState, disabled }: { audioState: TAudioState; disabled: boo
const { colors } = useTheme();
if (audioState === 'loading') {
return <RCActivityIndicator />;
return <RCActivityIndicator size={24} color={colors.buttonFontPrimary} />;
}
let customIconName: TCustomIconName = 'arrow-down';

View File

@ -4,11 +4,10 @@ import { Text } from 'react-native';
import styles from './styles';
import { useTheme } from '../../theme';
import { AUDIO_PLAYBACK_SPEED, AVAILABLE_SPEEDS } from './constants';
import { TAudioState } from './types';
import { useUserPreferences } from '../../lib/methods';
import NativeButton from '../NativeButton';
const PlaybackSpeed = ({ audioState }: { audioState: TAudioState }) => {
const PlaybackSpeed = () => {
const [playbackSpeed, setPlaybackSpeed] = useUserPreferences<number>(AUDIO_PLAYBACK_SPEED, AVAILABLE_SPEEDS[1]);
const { colors } = useTheme();
@ -20,7 +19,6 @@ const PlaybackSpeed = ({ audioState }: { audioState: TAudioState }) => {
return (
<NativeButton
disabled={audioState !== 'playing'}
onPress={onPress}
style={[styles.containerPlaybackSpeed, { backgroundColor: colors.buttonBackgroundSecondaryDefault }]}
>

View File

@ -10,7 +10,8 @@ import styles from './styles';
import Seek from './Seek';
import PlaybackSpeed from './PlaybackSpeed';
import PlayButton from './PlayButton';
import audioPlayer from '../../lib/methods/audioPlayer';
import EventEmitter from '../../lib/methods/helpers/events';
import audioPlayer, { AUDIO_FOCUSED } from '../../lib/methods/audioPlayer';
import { AUDIO_PLAYBACK_SPEED, AVAILABLE_SPEEDS } from './constants';
import { TDownloadState } from '../../lib/methods/handleMediaDownload';
import { TAudioState } from './types';
@ -39,6 +40,7 @@ const AudioPlayer = ({
const [playbackSpeed] = useUserPreferences<number>(AUDIO_PLAYBACK_SPEED, AVAILABLE_SPEEDS[1]);
const [paused, setPaused] = useState(true);
const [focused, setFocused] = useState(false);
const duration = useSharedValue(0);
const currentTime = useSharedValue(0);
const { colors } = useTheme();
@ -139,6 +141,15 @@ const AudioPlayer = ({
};
}, [navigation]);
useEffect(() => {
const listener = EventEmitter.addEventListener(AUDIO_FOCUSED, ({ audioFocused }: { audioFocused: string }) => {
setFocused(audioFocused === audioUri.current);
});
return () => {
EventEmitter.removeListener(AUDIO_FOCUSED, listener);
};
}, []);
let audioState: TAudioState = 'to-download';
if (isLoading) {
audioState = 'loading';
@ -154,7 +165,7 @@ const AudioPlayer = ({
<View style={[styles.audioContainer, { backgroundColor: colors.surfaceTint, borderColor: colors.strokeExtraLight }]}>
<PlayButton disabled={disabled} audioState={audioState} onPress={onPress} />
<Seek currentTime={currentTime} duration={duration} loaded={!disabled && isDownloaded} onChangeTime={setPosition} />
{audioState === 'playing' ? <PlaybackSpeed audioState={audioState} /> : null}
{audioState === 'playing' || focused ? <PlaybackSpeed /> : null}
</View>
);
};

View File

@ -1,5 +1,9 @@
import { AVPlaybackStatus, Audio, InterruptionModeAndroid, InterruptionModeIOS } from 'expo-av';
import EventEmitter from './helpers/events';
export const AUDIO_FOCUSED = 'AUDIO_FOCUSED';
const AUDIO_MODE = {
allowsRecordingIOS: false,
playsInSilentModeIOS: true,
@ -42,6 +46,7 @@ class AudioPlayer {
try {
await this.audioQueue[audioKey]?.stopAsync();
this.audioPlaying = '';
EventEmitter.emit(AUDIO_FOCUSED, { audioFocused: '' });
} catch {
// do nothing
}
@ -62,6 +67,7 @@ class AudioPlayer {
await Audio.setAudioModeAsync(AUDIO_MODE);
await this.audioQueue[audioKey]?.playAsync();
this.audioPlaying = audioKey;
EventEmitter.emit(AUDIO_FOCUSED, { audioFocused: audioKey });
}
async pauseAudio(audioKey: string) {

View File

@ -12,7 +12,8 @@ type TEventEmitterEmmitArgs =
| { visible: boolean; onCancel?: null | Function }
| { cancel: () => void }
| { submit: (param: string) => void }
| IEmitUserInteraction;
| IEmitUserInteraction
| { audioFocused: string };
class EventEmitter {
private events: { [key: string]: any };