mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-20 15:20:39 -07:00
Add audio sample components for recording, system capture, and upload
- Introduced `AudioSampleRecording`, `AudioSampleSystem`, and `AudioSampleUpload` components to handle audio recording, system audio capture, and file uploads respectively. - Implemented play/pause functionality for audio playback across all components, enhancing user interaction. - Refactored `ProfileForm` and `SampleUpload` to utilize new audio components, improving code organization and maintainability. - Added hooks for audio playback management, ensuring consistent audio handling and cleanup across the application.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { useRef, useState } from 'react';
|
||||
import { useToast } from '@/components/ui/use-toast';
|
||||
|
||||
export function useAudioPlayer() {
|
||||
const [isPlaying, setIsPlaying] = useState(false);
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const { toast } = useToast();
|
||||
|
||||
const playPause = (file: File | null | undefined) => {
|
||||
if (!file) return;
|
||||
|
||||
if (audioRef.current) {
|
||||
if (isPlaying) {
|
||||
audioRef.current.pause();
|
||||
setIsPlaying(false);
|
||||
} else {
|
||||
audioRef.current.play();
|
||||
setIsPlaying(true);
|
||||
}
|
||||
} else {
|
||||
const audio = new Audio(URL.createObjectURL(file));
|
||||
audioRef.current = audio;
|
||||
|
||||
audio.addEventListener('ended', () => {
|
||||
setIsPlaying(false);
|
||||
if (audioRef.current) {
|
||||
URL.revokeObjectURL(audioRef.current.src);
|
||||
}
|
||||
audioRef.current = null;
|
||||
});
|
||||
|
||||
audio.addEventListener('error', () => {
|
||||
setIsPlaying(false);
|
||||
toast({
|
||||
title: 'Playback error',
|
||||
description: 'Failed to play audio file',
|
||||
variant: 'destructive',
|
||||
});
|
||||
if (audioRef.current) {
|
||||
URL.revokeObjectURL(audioRef.current.src);
|
||||
}
|
||||
audioRef.current = null;
|
||||
});
|
||||
|
||||
audio.play();
|
||||
setIsPlaying(true);
|
||||
}
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
if (audioRef.current) {
|
||||
audioRef.current.pause();
|
||||
if (audioRef.current.src.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(audioRef.current.src);
|
||||
}
|
||||
audioRef.current = null;
|
||||
}
|
||||
setIsPlaying(false);
|
||||
};
|
||||
|
||||
return {
|
||||
isPlaying,
|
||||
playPause,
|
||||
cleanup,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user