Skip to content

Commit 57f67e7

Browse files
authored
fix: audio component recursive loading for expo (#3038)
1 parent ef46aac commit 57f67e7

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

package/src/components/Attachment/AudioAttachment.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,14 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
208208
if (!isExpoCLI) {
209209
return;
210210
}
211-
if (item.paused) {
212-
await pauseAudio();
213-
} else {
214-
await playAudio();
211+
try {
212+
if (item.paused) {
213+
await pauseAudio();
214+
} else {
215+
await playAudio();
216+
}
217+
} catch (e) {
218+
console.log('An error has occurred while trying to interact with the audio. ', e);
215219
}
216220
};
217221
// For expo CLI

package/src/hooks/useAudioPlayer.ts

+37-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useCallback } from 'react';
22

33
import { NativeHandlers, SoundReturnType } from '../native';
44

@@ -15,7 +15,7 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
1515

1616
const isExpoCLI = NativeHandlers.SDK === 'stream-chat-expo';
1717

18-
const playAudio = async () => {
18+
const playAudio = useCallback(async () => {
1919
if (isExpoCLI) {
2020
if (soundRef.current?.playAsync) {
2121
await soundRef.current.playAsync();
@@ -25,9 +25,9 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
2525
soundRef.current.resume();
2626
}
2727
}
28-
};
28+
}, [isExpoCLI, soundRef]);
2929

30-
const pauseAudio = async () => {
30+
const pauseAudio = useCallback(async () => {
3131
if (isExpoCLI) {
3232
if (soundRef.current?.pauseAsync) {
3333
await soundRef.current.pauseAsync();
@@ -37,39 +37,45 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
3737
soundRef.current.pause();
3838
}
3939
}
40-
};
40+
}, [isExpoCLI, soundRef]);
4141

42-
const seekAudio = async (currentTime: number) => {
43-
if (isExpoCLI) {
44-
if (currentTime === 0) {
45-
// If currentTime is 0, we should replay the video from 0th position.
46-
if (soundRef.current?.replayAsync) {
47-
await soundRef.current.replayAsync({
48-
positionMillis: 0,
49-
shouldPlay: false,
50-
});
42+
const seekAudio = useCallback(
43+
async (currentTime: number) => {
44+
if (isExpoCLI) {
45+
if (currentTime === 0) {
46+
// If currentTime is 0, we should replay the video from 0th position.
47+
if (soundRef.current?.replayAsync) {
48+
await soundRef.current.replayAsync({
49+
positionMillis: 0,
50+
shouldPlay: false,
51+
});
52+
}
53+
} else {
54+
if (soundRef.current?.setPositionAsync) {
55+
await soundRef.current.setPositionAsync(currentTime * 1000);
56+
}
5157
}
5258
} else {
53-
if (soundRef.current?.setPositionAsync) {
54-
await soundRef.current.setPositionAsync(currentTime * 1000);
59+
if (soundRef.current?.seek) {
60+
soundRef.current.seek(currentTime);
5561
}
5662
}
57-
} else {
58-
if (soundRef.current?.seek) {
59-
soundRef.current.seek(currentTime);
60-
}
61-
}
62-
};
63+
},
64+
[isExpoCLI, soundRef],
65+
);
6366

64-
const changeAudioSpeed = async (speed: number) => {
65-
// Handled through prop `rate` in `Sound.Player`
66-
if (!isExpoCLI) {
67-
return;
68-
}
69-
if (soundRef.current?.setRateAsync) {
70-
await soundRef.current.setRateAsync(speed);
71-
}
72-
};
67+
const changeAudioSpeed = useCallback(
68+
async (speed: number) => {
69+
// Handled through prop `rate` in `Sound.Player`
70+
if (!isExpoCLI) {
71+
return;
72+
}
73+
if (soundRef.current?.setRateAsync) {
74+
await soundRef.current.setRateAsync(speed);
75+
}
76+
},
77+
[isExpoCLI, soundRef],
78+
);
7379

7480
return { changeAudioSpeed, pauseAudio, playAudio, seekAudio };
7581
};

0 commit comments

Comments
 (0)