1- import { Audio } from 'expo-av' ;
1+ import { createAudioPlayer , setAudioModeAsync , requestRecordingPermissionsAsync } from 'expo-audio' ;
2+ import type { AudioPlayer as ExpoAudioPlayer } from 'expo-audio/build/AudioModule.types' ;
23import * as FileSystem from 'expo-file-system' ;
34import { VoiceProcessor } from '@picovoice/react-native-voice-processor' ;
45
@@ -274,10 +275,10 @@ export class AudioRecorder {
274275
275276/**
276277 * Classe per gestire la riproduzione di chunk audio PCM16
277- * Usa expo-av per il playback
278+ * Usa expo-audio per il playback
278279 */
279280export class AudioPlayer {
280- private currentSound : Audio . Sound | null = null ;
281+ private currentPlayer : ExpoAudioPlayer | null = null ;
281282 private chunkBuffer : { index ?: number ; data : string } [ ] = [ ] ;
282283 private seenChunkIndexes : Set < number > = new Set ( ) ;
283284 private highestIndexedChunk : number = - 1 ;
@@ -353,7 +354,7 @@ export class AudioPlayer {
353354
354355 console . log ( `AudioPlayer: ${ totalChunks } chunk -> ${ pcm16Data . length } bytes PCM16` ) ;
355356
356- // Wrappa in WAV per la riproduzione con expo-av
357+ // Wrappa in WAV per la riproduzione con expo-audio
357358 const wavData = wrapPcm16InWav ( pcm16Data , AUDIO_CONFIG . SAMPLE_RATE ) ;
358359 const wavBase64 = encodeBase64 ( wavData ) ;
359360
@@ -362,25 +363,23 @@ export class AudioPlayer {
362363 encoding : FileSystem . EncodingType . Base64 ,
363364 } ) ;
364365
365- await Audio . setAudioModeAsync ( {
366- allowsRecordingIOS : false ,
366+ await setAudioModeAsync ( {
367367 playsInSilentModeIOS : true ,
368- staysActiveInBackground : true ,
369- shouldDuckAndroid : true ,
370- playThroughEarpieceAndroid : false ,
368+ shouldPlayInBackground : true ,
369+ shouldRouteThroughEarpiece : false ,
371370 } ) ;
372371
373- const { sound } = await Audio . Sound . createAsync ( { uri : tempPath } ) ;
374- this . currentSound = sound ;
372+ const player = createAudioPlayer ( { uri : tempPath } ) ;
373+ this . currentPlayer = player ;
375374
376- this . currentSound . setOnPlaybackStatusUpdate ( async ( status ) => {
377- if ( status . isLoaded && status . didJustFinish ) {
375+ player . addListener ( 'playbackStatusUpdate' , async ( status ) => {
376+ if ( status . didJustFinish ) {
378377 console . log ( 'AudioPlayer: Riproduzione completata' ) ;
379378 await this . onPlaybackComplete ( onComplete , tempPath ) ;
380379 }
381380 } ) ;
382381
383- await this . currentSound . playAsync ( ) ;
382+ player . play ( ) ;
384383 this . isPlaying = true ;
385384 this . clearChunks ( ) ;
386385
@@ -404,13 +403,13 @@ export class AudioPlayer {
404403 }
405404
406405 private async onPlaybackComplete ( onComplete ?: ( ) => void , audioFilePath ?: string ) : Promise < void > {
407- if ( this . currentSound ) {
406+ if ( this . currentPlayer ) {
408407 try {
409- await this . currentSound . unloadAsync ( ) ;
408+ this . currentPlayer . remove ( ) ;
410409 } catch ( error ) {
411410 console . error ( 'Errore cleanup audio:' , error ) ;
412411 }
413- this . currentSound = null ;
412+ this . currentPlayer = null ;
414413 }
415414
416415 if ( audioFilePath ) {
@@ -426,14 +425,14 @@ export class AudioPlayer {
426425 }
427426
428427 async stopPlayback ( ) : Promise < void > {
429- if ( this . currentSound ) {
428+ if ( this . currentPlayer ) {
430429 try {
431- await this . currentSound . stopAsync ( ) ;
432- await this . currentSound . unloadAsync ( ) ;
430+ this . currentPlayer . pause ( ) ;
431+ this . currentPlayer . remove ( ) ;
433432 } catch ( error ) {
434433 console . error ( 'Errore stop riproduzione:' , error ) ;
435434 }
436- this . currentSound = null ;
435+ this . currentPlayer = null ;
437436 }
438437 this . isPlaying = false ;
439438 }
@@ -473,7 +472,7 @@ export function base64ToArrayBuffer(base64: string): ArrayBuffer {
473472 */
474473export async function checkAudioPermissions ( ) : Promise < boolean > {
475474 try {
476- const { status } = await Audio . requestPermissionsAsync ( ) ;
475+ const { status } = await requestRecordingPermissionsAsync ( ) ;
477476 return status === 'granted' ;
478477 } catch ( error ) {
479478 console . error ( 'Errore controllo permessi audio:' , error ) ;
0 commit comments