-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve audio playback and audio/text timing
- Loading branch information
1 parent
386ac9d
commit 2ed74fd
Showing
5 changed files
with
101 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,56 @@ | ||
// In the AudioOutput component | ||
import React, { useEffect, useRef } from "react"; | ||
|
||
function AudioOutput({ currentAudioMessage }) { | ||
function AudioOutput({ currentAudioMessage, isPaused }) { | ||
const audioRef = useRef(null); | ||
const urlRef = useRef(null); | ||
|
||
useEffect(() => { | ||
// Initialize the audio element if it does not exist | ||
if (!audioRef.current) { | ||
audioRef.current = new Audio(); | ||
} | ||
return () => { | ||
audioRef.current && audioRef.current.pause(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
// Handle updating the audio source when the message changes | ||
if (currentAudioMessage) { | ||
// Revoke the old URL to avoid memory leaks | ||
if (urlRef.current?.url) { | ||
URL.revokeObjectURL(urlRef.current.url); | ||
} | ||
// Create a new URL for the updated audio blob | ||
const blob = new Blob([currentAudioMessage.audio], { type: "audio/mp3" }); | ||
const url = URL.createObjectURL(blob); | ||
audioRef.current.src = url; | ||
urlRef.current = { | ||
url: URL.createObjectURL(blob), | ||
id: currentAudioMessage.id, | ||
}; | ||
audioRef.current.src = urlRef.current.url; | ||
audioRef.current.load(); | ||
|
||
// Auto-play the new audio if not paused | ||
if (!isPaused) { | ||
audioRef.current | ||
.play() | ||
.catch((err) => console.error("Error playing audio:", err)); | ||
} | ||
} | ||
}, [currentAudioMessage]); | ||
|
||
useEffect(() => { | ||
// Manage playback based on isPaused state | ||
if (!isPaused && currentAudioMessage) { | ||
audioRef.current | ||
.play() | ||
.catch((err) => console.error("Error playing audio:", err)); | ||
|
||
return () => { | ||
URL.revokeObjectURL(url); | ||
}; | ||
} else { | ||
audioRef.current.pause(); | ||
} | ||
}, [currentAudioMessage]); | ||
}, [isPaused]); | ||
|
||
return ( | ||
<audio | ||
ref={audioRef} | ||
controls | ||
autoPlay | ||
/> | ||
); | ||
return null; // This component does not render anything itself | ||
} | ||
|
||
export default AudioOutput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters