2011-02-28 77 views
0

我有一個按鈕,當我點擊它表明我用圖片的幻燈片,從我的SD卡播放音頻,此代碼:的Android結結巴巴音頻

public void buttonClickHandler(View v) { 
    this.onClickNextDiapositiva(); 
    Diapositiva diapo1 = this.getDiapoActual(); 
    try { 
     if (diapo1.tieneSonido()) { 
      String sndPath = ZIP_SND_PATH + diapo1.getSonido().getNombre(); 
      InputStream isSonido = this.getTutorFile().getFile(sndPath); 

      this.audioPlayer = new StreamingMediaPlayer(this); 
      this.audioPlayer.startStreaming(isSonido); 
     } else if (diapo1.tieneVideo()) { 
      if (!diapo1.tieneImagen()) { 
      String imgPath = ZIP_FONDOS_PATH + "fondo_video.png"; 
      cargarImagen(imgPath); 
      } 
     } 
    } catch (Throwable ex) { 
     Log.e("mostrarDiapositiva", ex.getMessage()); 
     Toast 
     .makeText(this, "Error: " + ex.getMessage(), Toast.LENGTH_SHORT) 
     .show(); 
    } 
    break; 

}

事實是,代碼起作用,幻燈片被改變並且音頻播放,但是它開始播放,並且當播放不到一秒鐘時,它再次開始,這就像是結束了。

任何想法爲什麼會發生這種情況?感謝太多

回答

1

使用的MediaPlayer,而不是StreamingMediaPlayer

例如:

MediaPlayer mp = new MediaPlayer(); 
// when you want to play the sound stored in nodeaudio: 
// nodeaudio is a path like /min/sdcard/soundfile 

if (nodeaudio == null || nodeaudio.trim().equals("")) {     
    mp.reset();    
} else {     
    try {     
     mp.reset();     
     mp.setDataSource(nodeaudio);     
     mp.prepare();     
     mp.start();     
    }     
    catch(Exception e) {      
     Log.e("Play sound ERROR", e.toString());      
     e.printStackTrace();     
    }    
}