Sound in android (StreamingMediaPlayer not working properly. Sluttered)
private void mostrarDiapositiva(Vector<Diapositiva> d, int pos) {
if ( pos >= d.size() ) {
pos = d.size() - 1;
}
Diapositiva diapo = d.get(pos);
this.setDiapoPosActual(pos);
this.setDiapoActual(diapo);
reiniciarDiapositiva();
mostrarDiapositiva(diapo);
try {
if (diapo.tieneSonido()) {
String sndPath = ZIP_SND_PATH + diapo.getSonido().getNombre();
InputStream isSonido = this.getTutorFile().getFile(sndPath);
this.audioPlayer = new StreamingMediaPlayer(this);
this.audioPlayer.startStreaming(isSonido);
} else if (diapo.tieneVideo()) {
if (!diapo.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();
}
}
private void mostrarDiapositiva(Diapositiva diapo) 开发者_JS百科{
ImageButton bp = (ImageButton) findViewById(R.id.buttonprev);
bp.setImageResource(R.drawable.ic_menu_back);
ImageButton bn = (ImageButton) findViewById(R.id.buttonnext);
bn.setImageResource(R.drawable.ic_menu_forward);
try {
if (diapo.tieneImagen()) {
String imgPath = ZIP_IMG_PATH + diapo.getImagen().getNombre();
cargarImagen(imgPath);
}
this.animarFade(this.FLIPPER_DIAPOS_NUMLAYOUT);
//if (diapo.tieneSonido()) {
// String sndPath = ZIP_SND_PATH + diapo.getSonido().getNombre();
// InputStream isSonido = this.getTutorFile().getFile(sndPath);
} catch (Throwable ex) {
Log.e("mostrarDiapositiva", ex.getMessage());
Toast
.makeText(this, "Error: " + ex.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
and everithing works fine except the sound. When I paint the screen
mostrarDiapositiva(diapo); we have an associated sound for every slide so if it has sound (if (diapo.tieneSonido())) it´s plays it.
the problem is that the very first time the audio starts after one or two syllable it starts again.
it´s seems like the audio starts when the slide is being painted and when it is painted it starts again. There any doevents or somethink like that?
thanks
This works in a similar case:
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();
}
}
精彩评论