Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions src/hooks/VoicePlayer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,25 @@ export const VoicePlayerProvider = ({
}

logger.info('VoicePlayer: Start getting audio file.');
new Promise((resolve) => {
new Promise<File>((resolve) => {
voicePlayerDispatcher({
type: INITIALIZE_AUDIO_UNIT,
payload: { groupKey },
});
// audio file passed as a parameter
if (audioFile) {
resolve(audioFile);
logger.info('VoicePlayer: Use the audioFile instance.');
resolve(audioFile);
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is audioFile, no need to fetch from backend

}
if (audioStorage?.[groupKey]?.audioFile) {
resolve(audioStorage[groupKey].audioFile);
// audio file from the audioStorage
const cachedAudioFile = audioStorage?.[groupKey]?.audioFile;
if (cachedAudioFile) {
logger.info('VoicePlayer: Get from the audioStorage.');
resolve(cachedAudioFile);
return;
}
voicePlayerDispatcher({
type: INITIALIZE_AUDIO_UNIT,
payload: { groupKey },
});
// fetch the audio file from URL
fetch(audioFileUrl)
.then((res) => res.blob())
.then((blob) => {
Expand All @@ -118,7 +124,8 @@ export const VoicePlayerProvider = ({
logger.info('VoicePlayer: Get the audioFile from URL.');
});
}).then((audioFile: File) => {
logger.info('VoicePlayer: Succeeded getting audio file.', audioFile);
const voicePlayerRoot = document.getElementById(VOICE_PLAYER_ROOT_ID);
logger.info('VoicePlayer: Succeeded getting audio file.', { audioFile });
const currentAudioUnit = audioStorage[groupKey] || AudioUnitDefaultValue() as AudioStorageUnit;
const audioPlayer = new Audio(URL?.createObjectURL?.(audioFile));
audioPlayer.id = VOICE_PLAYER_AUDIO_ID;
Expand All @@ -145,14 +152,31 @@ export const VoicePlayerProvider = ({
payload: { groupKey },
});
};
audioPlayer?.play();
const voicePlayerRoot = document.getElementById(VOICE_PLAYER_ROOT_ID);
voicePlayerRoot.appendChild(audioPlayer);
voicePlayerDispatcher({
type: SET_CURRENT_PLAYER,
payload: { groupKey, audioPlayer },
});
logger.info('VoicePlayer: Succeeded playing audio player.', { groupKey, audioPlayer });
audioPlayer.dataset.sbGroupId = groupKey;
// clean up the previous audio player
try {
voicePlayerRoot?.childNodes.forEach((node) => {
const element = node as HTMLAudioElement;
const thisGroupKey = element.dataset?.sbGroupKey;
if (thisGroupKey !== groupKey) {
element?.pause?.();
voicePlayerDispatcher({
type: ON_VOICE_PLAYER_PAUSE,
payload: { groupKey },
});
voicePlayerRoot.removeChild(element);
logger.info('VoicePlayer: Removed other player.', { element });
}
});
} finally {
audioPlayer?.play();
voicePlayerRoot?.appendChild(audioPlayer);
voicePlayerDispatcher({
type: SET_CURRENT_PLAYER,
payload: { groupKey, audioPlayer },
});
logger.info('VoicePlayer: Succeeded playing audio player.', { groupKey, audioPlayer });
}
});
};

Expand Down