mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-10-03 09:05:17 -07:00
Refactor AudioPlayer and related components to support conditional auto-play functionality
- Updated AudioPlayer to auto-play only if the shouldAutoPlay flag is set, enhancing user control over playback. - Refactored HistoryTable, SampleList, and useGenerationForm to utilize setAudioWithAutoPlay for consistent audio loading and playback behavior. - Improved user experience by ensuring audio is only played when explicitly intended, reducing unexpected playback.
This commit is contained in:
@@ -357,14 +357,22 @@ export function AudioPlayer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standard WaveSurfer auto-play
|
// Only auto-play if shouldAutoPlay flag is set (user explicitly clicked to play)
|
||||||
// Use a small delay to ensure audio element is fully ready
|
const shouldAutoPlayNow = usePlayerStore.getState().shouldAutoPlay;
|
||||||
setTimeout(() => {
|
if (shouldAutoPlayNow) {
|
||||||
wavesurfer.play().catch((error) => {
|
// Clear the flag first
|
||||||
debug.error('Failed to autoplay:', error);
|
usePlayerStore.getState().clearAutoPlayFlag();
|
||||||
// Don't show error for autoplay failures (browser restrictions)
|
|
||||||
});
|
// Use a small delay to ensure audio element is fully ready
|
||||||
}, 100);
|
setTimeout(() => {
|
||||||
|
wavesurfer.play().catch((error) => {
|
||||||
|
debug.error('Failed to autoplay:', error);
|
||||||
|
// Don't show error for autoplay failures (browser restrictions)
|
||||||
|
});
|
||||||
|
}, 100);
|
||||||
|
} else {
|
||||||
|
debug.log('Skipping auto-play - shouldAutoPlay is false');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle play/pause
|
// Handle play/pause
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export function HistoryTable() {
|
|||||||
const exportGeneration = useExportGeneration();
|
const exportGeneration = useExportGeneration();
|
||||||
const exportGenerationAudio = useExportGenerationAudio();
|
const exportGenerationAudio = useExportGenerationAudio();
|
||||||
const importGeneration = useImportGeneration();
|
const importGeneration = useImportGeneration();
|
||||||
const setAudio = usePlayerStore((state) => state.setAudio);
|
const setAudioWithAutoPlay = usePlayerStore((state) => state.setAudioWithAutoPlay);
|
||||||
const restartCurrentAudio = usePlayerStore((state) => state.restartCurrentAudio);
|
const restartCurrentAudio = usePlayerStore((state) => state.restartCurrentAudio);
|
||||||
const currentAudioId = usePlayerStore((state) => state.audioId);
|
const currentAudioId = usePlayerStore((state) => state.audioId);
|
||||||
const isPlaying = usePlayerStore((state) => state.isPlaying);
|
const isPlaying = usePlayerStore((state) => state.isPlaying);
|
||||||
@@ -77,9 +77,9 @@ export function HistoryTable() {
|
|||||||
if (currentAudioId === audioId) {
|
if (currentAudioId === audioId) {
|
||||||
restartCurrentAudio();
|
restartCurrentAudio();
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, load the new audio
|
// Otherwise, load the new audio and auto-play it
|
||||||
const audioUrl = apiClient.getAudioUrl(audioId);
|
const audioUrl = apiClient.getAudioUrl(audioId);
|
||||||
setAudio(audioUrl, audioId, profileId, text.substring(0, 50));
|
setAudioWithAutoPlay(audioUrl, audioId, profileId, text.substring(0, 50));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -233,7 +233,11 @@ export function HistoryTable() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Far right - Ellipsis actions */}
|
{/* Far right - Ellipsis actions */}
|
||||||
<div className="w-10 shrink-0 flex justify-end">
|
<div
|
||||||
|
className="w-10 shrink-0 flex justify-end"
|
||||||
|
onMouseDown={(e) => e.stopPropagation()}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<Button
|
<Button
|
||||||
@@ -241,7 +245,6 @@ export function HistoryTable() {
|
|||||||
size="icon"
|
size="icon"
|
||||||
className="h-8 w-8"
|
className="h-8 w-8"
|
||||||
aria-label="Actions"
|
aria-label="Actions"
|
||||||
onClick={(e) => e.stopPropagation()}
|
|
||||||
>
|
>
|
||||||
<MoreHorizontal className="h-4 w-4" />
|
<MoreHorizontal className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export function SampleList({ profileId }: SampleListProps) {
|
|||||||
const { data: samples, isLoading } = useProfileSamples(profileId);
|
const { data: samples, isLoading } = useProfileSamples(profileId);
|
||||||
const deleteSample = useDeleteSample();
|
const deleteSample = useDeleteSample();
|
||||||
const [uploadOpen, setUploadOpen] = useState(false);
|
const [uploadOpen, setUploadOpen] = useState(false);
|
||||||
const setAudio = usePlayerStore((state) => state.setAudio);
|
const setAudioWithAutoPlay = usePlayerStore((state) => state.setAudioWithAutoPlay);
|
||||||
const currentAudioId = usePlayerStore((state) => state.audioId);
|
const currentAudioId = usePlayerStore((state) => state.audioId);
|
||||||
const isPlaying = usePlayerStore((state) => state.isPlaying);
|
const isPlaying = usePlayerStore((state) => state.isPlaying);
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ export function SampleList({ profileId }: SampleListProps) {
|
|||||||
|
|
||||||
const handlePlay = (referenceText: string, sampleId: string) => {
|
const handlePlay = (referenceText: string, sampleId: string) => {
|
||||||
const audioUrl = apiClient.getSampleUrl(sampleId);
|
const audioUrl = apiClient.getSampleUrl(sampleId);
|
||||||
setAudio(audioUrl, sampleId, referenceText.substring(0, 50));
|
setAudioWithAutoPlay(audioUrl, sampleId, null, referenceText.substring(0, 50));
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ interface UseGenerationFormOptions {
|
|||||||
export function useGenerationForm(options: UseGenerationFormOptions = {}) {
|
export function useGenerationForm(options: UseGenerationFormOptions = {}) {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const generation = useGeneration();
|
const generation = useGeneration();
|
||||||
const setAudio = usePlayerStore((state) => state.setAudio);
|
const setAudioWithAutoPlay = usePlayerStore((state) => state.setAudioWithAutoPlay);
|
||||||
const setIsGenerating = useGenerationStore((state) => state.setIsGenerating);
|
const setIsGenerating = useGenerationStore((state) => state.setIsGenerating);
|
||||||
const [downloadingModelName, setDownloadingModelName] = useState<string | null>(null);
|
const [downloadingModelName, setDownloadingModelName] = useState<string | null>(null);
|
||||||
const [downloadingDisplayName, setDownloadingDisplayName] = useState<string | null>(null);
|
const [downloadingDisplayName, setDownloadingDisplayName] = useState<string | null>(null);
|
||||||
@@ -97,7 +97,7 @@ export function useGenerationForm(options: UseGenerationFormOptions = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const audioUrl = apiClient.getAudioUrl(result.id);
|
const audioUrl = apiClient.getAudioUrl(result.id);
|
||||||
setAudio(audioUrl, result.id, selectedProfileId, data.text.substring(0, 50));
|
setAudioWithAutoPlay(audioUrl, result.id, selectedProfileId, data.text.substring(0, 50));
|
||||||
|
|
||||||
form.reset();
|
form.reset();
|
||||||
options.onSuccess?.(result.id);
|
options.onSuccess?.(result.id);
|
||||||
|
|||||||
Reference in New Issue
Block a user