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:
Jamie Pine
2026-01-26 16:44:49 -08:00
parent 834323068d
commit b1cf7926c7
6 changed files with 568 additions and 731 deletions
+66
View File
@@ -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,
};
}