android - Wait for a sound to end playing. Is this correct? -
in application need play sound in different moments , wait end before doing else. right have code playing sound:
private mediaplayer mplayer = null; private boolean playsound(){ mplayer = new mediaplayer(); try{ mplayer.setdatasource(openfileinput(filename).getfd()); mplayer.prepare(); mplayer.start(); } catch (exception e){ e.printstacktrace(); log.e("player", "error playing sound: "+filename); return false; } while(mplayer.isplaying()); mplayer.release(); mplayer = null; return true; }
it's not working expected: when changing image before playing sound change happens after sound played (i suppose it's thread queue or this)
i don't know if bad solution or if there better solution case:
- the sound playing must start after previous work finished.
- the app must not other work while playing sound.
- the code execute after sound playing not same.
you should implement completion listener:
public class myclass extends .... implements oncompletionlistener, ... { private int nextaction; // not smatest solution, enough :) ... private boolean playsound(next){ nextaction = next; mplayer = new mediaplayer(); try{ mplayer.setdatasource(openfileinput(filename).getfd()); mplayer.prepare(); mplayer.setoncompletionlistener(this); mplayer.start(); } catch (exception e){ e.printstacktrace(); log.e("player", "error playing sound: "+filename); return false; } @override public void oncompletion(mediaplayer mp) { // stuff, destroy mplayer, if needed switch(nextaction){ case action1: ...; break; case action2: ...; break; //..and on } }
Comments
Post a Comment