2012-03-07 11 views
45

Ho bisogno di ottenere la durata di un file audio per una serie di annunci vocali che devono essere riprodotti da un'app. Ho aggiunto i file audio come risorse e funzionano bene. Il seguente codice di esempio funziona perfettamente per lo scopo previsto: restituisce la durata dei file audio.Android: mediaplayer è andato via con eventi non gestiti

Ecco il codice:

float getDurationOfAudioResource(LocationEnum loc, Context context){ 
    float duration = 0; 
    try { 
     MediaPlayer mp; 
     mp = MediaPlayer.create(context, getAudioResource(loc)); 
     duration = mp.getDuration(); 
     mp.release(); 
     mp = null; 
    } 
    catch (IllegalStateException e) {e.printStackTrace(); logError(25, "TestDescItem:Fault::Could not open mediaplayer object with audio resource.");} 
    return duration; 
} 

Ecco la cosa strana. Questo codice viene chiamato in un'attività principale che prepara l'insieme di istruzioni audio per un determinato test. Non ci sono errori in questa attività. Ma non appena viene chiamata la seconda attività, ottengo una lunga serie di errori su logcat.

03-07 13:23:43.820: I/ActionLogger(21435): GenTest_Info_Test #0 successfully created. 
03-07 13:23:43.830: I/ActionLogger(21435): GenTest_Info_Test #1 successfully created. 
03-07 13:23:43.840: I/ActionLogger(21435): GenTest_Info_Test #2 successfully created. 
03-07 13:23:43.850: I/ActionLogger(21435): GenTest_Info_Test #3 successfully created. 
<snip> 
03-07 13:23:43.910: I/ActionLogger(21435): GenTest_Info_all tests successfully created. 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
03-07 13:23:47.270: W/MediaPlayer(21435): mediaplayer went away with unhandled events 
<snip> 

Ho un solo fatto un passo fino alla fine dell'attività principale (nessun errore) e dalla prima riga della seconda attività. Gli errori sicuramente vengono lanciati tra le attività.
Inoltre, se commento le otto righe del blocco try (restituendo solo zero) gli errori logcat vengono evitati. Quando ripristino le otto righe, gli errori tornano. Ho scavato nella documentazione e ho cercato nel web, e credo che sto costruendo, rilasciando e distruggendo correttamente l'oggetto mediaplayer, quindi non riesco a capire perché sto ricevendo un errore. Detto questo, devo fare qualcosa di sbagliato. Qualche idea?

Grazie,

Kevin

risposta

160

Appena messo mp.reset(); prima mp.release();.

+0

eccellente. Questo l'ha risolto! Grazie! – Hephaestus

+9

Che cosa fa? La release – Casebash

+4

causa l'arresto anomalo della mia videoview con IllegalStateException in seguito ... – Ron

34

Il santo cinque:

if(mp!=null) { 
     if(mp.isPlaying()) 
      mp.stop(); 
     mp.reset(); 
     mp.release(); 
     mp=null; 
    } 
+2

Ha funzionato come un fascino – locrizak

+0

Per chiunque usi VideoView, questo è simile a quello che fa in 'stopPlayback();' – vvolkgang

Problemi correlati