Skip to content

Commit 6db1d0e

Browse files
committed
fix: expo audio player
1 parent 86b0063 commit 6db1d0e

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

package/src/components/Attachment/AudioAttachment.tsx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,16 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
7575
};
7676

7777
const handlePlayPause = async () => {
78-
if (isExpoCLI) {
79-
if (item.paused) {
78+
if (item.paused) {
79+
if (isExpoCLI) {
8080
await playAudio();
81-
} else {
82-
await pauseAudio();
8381
}
82+
onPlayPause(item.id, false);
8483
} else {
85-
if (item.paused) {
86-
onPlayPause(item.id, false);
87-
} else {
88-
onPlayPause(item.id, true);
84+
if (isExpoCLI) {
85+
await pauseAudio();
8986
}
87+
onPlayPause(item.id, true);
9088
}
9189
};
9290

@@ -96,17 +94,15 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
9694
await seekAudio(0);
9795
if (isExpoCLI) {
9896
await pauseAudio();
99-
} else {
100-
onPlayPause(item.id, true);
10197
}
98+
onPlayPause(item.id, true);
10299
};
103100

104101
const dragStart = async () => {
105102
if (isExpoCLI) {
106103
await pauseAudio();
107-
} else {
108-
onPlayPause(item.id, true);
109104
}
105+
onPlayPause(item.id, true);
110106
};
111107

112108
const dragProgress = (progress: number) => {
@@ -117,9 +113,8 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
117113
await seekAudio(progress * (item.duration as number));
118114
if (isExpoCLI) {
119115
await playAudio();
120-
} else {
121-
onPlayPause(item.id, false);
122116
}
117+
onPlayPause(item.id, false);
123118
};
124119

125120
/** For Expo CLI */
@@ -137,10 +132,9 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
137132
}
138133
// Update your UI for the loaded state
139134
if (playbackStatus.isPlaying) {
140-
onPlayPause(item.id, false);
141135
onProgress(item.id, positionMillis / durationMillis);
142136
} else {
143-
onPlayPause(item.id, true);
137+
// Update your UI for the paused state
144138
}
145139

146140
if (playbackStatus.isBuffering) {
@@ -179,6 +173,24 @@ export const AudioAttachment = (props: AudioAttachmentProps) => {
179173
// eslint-disable-next-line react-hooks/exhaustive-deps
180174
}, []);
181175

176+
// This is needed for expo applications where the rerender doesn't occur on time thefore you need to update the state of the sound.
177+
useEffect(() => {
178+
const initalPlayPause = async () => {
179+
if (!isExpoCLI) {
180+
return;
181+
}
182+
if (item.paused) {
183+
await pauseAudio();
184+
} else {
185+
await playAudio();
186+
}
187+
};
188+
// For expo CLI
189+
if (!Sound.Player) {
190+
initalPlayPause();
191+
}
192+
}, [item.paused]);
193+
182194
const onSpeedChangeHandler = async () => {
183195
if (currentSpeed === 2.0) {
184196
setCurrentSpeed(1.0);

0 commit comments

Comments
 (0)