@@ -244,12 +244,14 @@ export function useVoiceChat() {
244244 break ;
245245
246246 case 'audio_end' :
247- // Server ha finito di inviare chunk audio per questo segmento
247+ // Server ha finito di inviare chunk audio per questo segmento.
248+ // Con TrackPlayer la riproduzione è già in corso in streaming.
249+ // Segnaliamo che non arriveranno altri chunk per gestire il completamento.
248250 if ( audioPlayerRef . current && audioPlayerRef . current . getChunksCount ( ) > 0 ) {
249251 setState ( 'speaking' ) ;
250- console . log ( `[useVoiceChat] Avvio riproduzione audio (${ audioPlayerRef . current . getChunksCount ( ) } chunk) - mantengo auto-mute ` ) ;
251- audioPlayerRef . current . playPcm16Chunks ( ( ) => {
252- console . log ( '[useVoiceChat] Riproduzione completata' ) ;
252+ console . log ( `[useVoiceChat] audio_end ricevuto (${ audioPlayerRef . current . getChunksCount ( ) } chunk) - riproduzione streaming in corso ` ) ;
253+ audioPlayerRef . current . signalAllChunksReceived ( ( ) => {
254+ console . log ( '[useVoiceChat] Riproduzione streaming completata' ) ;
253255 // Riattiva il microfono SOLO se l'agent ha finito completamente
254256 if ( agentEndedRef . current ) {
255257 console . log ( '[useVoiceChat] Agent finito, auto-unmute e riavvio registrazione' ) ;
@@ -322,8 +324,14 @@ export function useVoiceChat() {
322324
323325 onAudioChunk : ( audioData : string , chunkIndex : number ) => {
324326 if ( audioPlayerRef . current ) {
325- audioPlayerRef . current . addChunk ( audioData , chunkIndex ) ;
327+ // Aggiunge il chunk alla queue di TrackPlayer e avvia riproduzione streaming
328+ audioPlayerRef . current . addChunk ( audioData , chunkIndex ) . catch ( err => {
329+ console . error ( '[useVoiceChat] Errore aggiunta chunk a TrackPlayer:' , err ) ;
330+ } ) ;
326331 setChunksReceived ( prev => prev + 1 ) ;
332+
333+ // Transiziona a 'speaking' al primo chunk ricevuto
334+ setState ( prev => prev !== 'speaking' ? 'speaking' : prev ) ;
327335 }
328336 } ,
329337
0 commit comments