Skip to content

Commit ea61658

Browse files
Merge pull request #140 from SimformSolutionsPvtLtd/fix/UNT-T30779_more_number_of_files_android_audio_doesnt_play
fix(UNT-T30779): audio doesnt play when more number of files
2 parents a7e8112 + 0ebfb3c commit ea61658

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/components/Waveform/Waveform.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
6868
const scrollRef = useRef<ScrollView>(null);
6969
const isLayoutCalculated = useRef<boolean>(false);
7070
const isAutoPaused = useRef<boolean>(false);
71+
const isAudioPlaying = useRef<boolean>(false);
7172
const [waveform, setWaveform] = useState<number[]>([]);
7273
const [viewLayout, setViewLayout] = useState<LayoutRectangle | null>(null);
7374
const [seekPosition, setSeekPosition] = useState<NativeTouchEvent | null>(
@@ -215,6 +216,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
215216
const result = await stopPlayer({
216217
playerKey: `PlayerFor${path}`,
217218
});
219+
isAudioPlaying.current = false;
218220
if (!isNil(result) && result) {
219221
if (resetProgress) {
220222
setCurrentProgress(0);
@@ -240,6 +242,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
240242
const startPlayerAction = async (args?: IStartPlayerRef) => {
241243
if (mode === 'static') {
242244
try {
245+
isAudioPlaying.current = true;
243246
if (playerState === PlayerState.stopped) {
244247
await preparePlayerForPath(currentProgress);
245248
}
@@ -278,6 +281,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
278281
const pausePlayerAction = async (changePlayerState = true) => {
279282
if (mode === 'static') {
280283
try {
284+
isAudioPlaying.current = false;
281285
const pause = await pausePlayer({
282286
playerKey: `PlayerFor${path}`,
283287
});
@@ -474,8 +478,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
474478
const tracePlayerState = onDidFinishPlayingAudio(async data => {
475479
if (data.playerKey === `PlayerFor${path}`) {
476480
if (data.finishType === FinishMode.stop) {
477-
setPlayerState(PlayerState.stopped);
478-
setCurrentProgress(0);
481+
stopPlayerAction();
479482
}
480483
}
481484
});
@@ -600,6 +603,20 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
600603
}
601604
}, [currentProgress, songDuration, onCurrentProgressChange]);
602605

606+
/* Ensure that the audio player is released (or stopped) once the song's duration is determined,
607+
especially if the audio is not playing immediately after loading */
608+
useEffect(() => {
609+
if (
610+
songDuration !== 0 &&
611+
mode === 'static' &&
612+
isAudioPlaying.current !== true
613+
) {
614+
isAudioPlaying.current = false;
615+
stopPlayerAction(false);
616+
}
617+
// eslint-disable-next-line react-hooks/exhaustive-deps
618+
}, [songDuration]);
619+
603620
useImperativeHandle(ref, () => ({
604621
startPlayer: startPlayerAction,
605622
stopPlayer: stopPlayerAction,

0 commit comments

Comments
 (0)