import React, { useState, useEffect } from 'react'; import { View, Text, Pressable } from 'react-native'; import { Icon } from '../ui/Icon'; import { TTSService } from '../../utils/ttsService'; import { Card as CardType } from '../../store/cardStore'; interface AudioCardProps { card: CardType; autoPlay?: boolean; showControls?: boolean; } export const AudioCard: React.FC = ({ card, autoPlay = false, showControls = true, }) => { const [isPlaying, setIsPlaying] = useState(false); const [rate, setRate] = useState(1.0); const [error, setError] = useState(null); useEffect(() => { if (autoPlay) { handlePlay(); } return () => { // Stop any ongoing speech when component unmounts TTSService.stop(); }; }, [card.id]); const handlePlay = async () => { try { setError(null); setIsPlaying(true); await TTSService.speakCard(card); setIsPlaying(false); } catch (error) { console.error('Error playing audio:', error); setError('Fehler beim Abspielen'); setIsPlaying(false); } }; const handleStop = async () => { try { await TTSService.stop(); setIsPlaying(false); } catch (error) { console.error('Error stopping audio:', error); } }; const handlePause = async () => { try { await TTSService.pause(); setIsPlaying(false); } catch (error) { console.error('Error pausing audio:', error); } }; const handleResume = async () => { try { await TTSService.resume(); setIsPlaying(true); } catch (error) { console.error('Error resuming audio:', error); } }; const adjustRate = (newRate: number) => { setRate(newRate); // Rate will be applied on next play }; if (!showControls) { return null; } return ( {/* Play/Pause Button */} pressed && { opacity: 0.7 }}> {/* Stop Button */} {isPlaying && ( pressed && { opacity: 0.7 }}> )} {/* Speed Controls */} Geschwindigkeit: adjustRate(0.75)} className={`rounded px-2 py-1 ${rate === 0.75 ? 'bg-blue-500' : 'bg-gray-200'}`} style={({ pressed }) => pressed && { opacity: 0.7 }}> 0.75x adjustRate(1.0)} className={`rounded px-2 py-1 ${rate === 1.0 ? 'bg-blue-500' : 'bg-gray-200'}`} style={({ pressed }) => pressed && { opacity: 0.7 }}> 1x adjustRate(1.25)} className={`rounded px-2 py-1 ${rate === 1.25 ? 'bg-blue-500' : 'bg-gray-200'}`} style={({ pressed }) => pressed && { opacity: 0.7 }}> 1.25x {/* Error Display */} {error && {error}} ); };