audio - Playing just one sound android -
i'm developing little quizz app , play sound depending on if user answers question correctly or not.
i've got many sound files in "assets/".
what play 1 of these sounds. i've been able play sound, not one, both sound (correct , incorrect) played 1 after other.
how can play one? here's source:
public void audioplayer(){ //set mediaplayer mp = new mediaplayer(); try { string mp3file = "correct.mp3"; assetmanager assetman = getassets(); fileinputstream mp3stream = assetman.openfd(mp3file).createinputstream(); mp.setdatasource(mp3stream.getfd()); mp.prepare(); mp.start(); } catch (exception e) { e.printstacktrace(); } }
put sound files i.e fail , applause in raw folder , use following code :
private assetfiledescriptor splaysounddescriptor = null; private assetfiledescriptor sfailsounddescriptor = null; private mediaplayer mp = null; public static final int fail_sound = 1; public static final int applause_sound = 2; public void audioplayer(int soundtype){ //set mediaplayer mp = new mediaplayer(); assetfiledescriptor soundfiledescriptor = null; try { if (sfailsounddescriptor == null) { sfailsounddescriptor = context.getresources(). openrawresourcefd(r.raw.fail); } if (sapplausesounddescriptor == null) { sapplausesounddescriptor = context.getresources(). openrawresourcefd(r.raw.applause); } switch (soundtype) { case fail_sound: soundfiledescriptor = sfailsounddescriptor; break; case applause_sound: soundfiledescriptor = sapplausesounddescriptor; break; } mp.reset(); mp.setdatasource(soundfiledescriptor.getfiledescriptor(), soundfiledescriptor.getstartoffset(), soundfiledescriptor.getdeclaredlength()); mp.prepare(); mp.setonpreparedlistener(new onpreparedlistener() { @override public void onprepared(mediaplayer player) { player.seekto(0); player.start(); } }); } catch (exception e) { e.printstacktrace(); } }
Comments
Post a Comment