android - Error while trying to start media player: start called in state error (-38, 0) -
i have gridview positions , case on oncreate, so,
case position = 1 play audio1,
case position = 2 play audio2
but when execute, diplay error , stop play sounds.
start called in state error (-38, 0)
the activity:
public class homeenglishfoodsactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_english_foods); gridview gridview = (gridview) findviewbyid(r.id.gridviewhomeenglishfoods); gridview.setadapter(new adapterhomeenglishfoodsactivity(this)); final mediaplayer mpapple = mediaplayer.create(this, r.raw.apple); final mediaplayer mpbeer = mediaplayer.create(this, r.raw.beer); final mediaplayer mpcoffee = mediaplayer.create(this, r.raw.coffee); final mediaplayer mpcheese = mediaplayer.create(this, r.raw.cheese); final mediaplayer mpfrenchfries = mediaplayer.create(this, r.raw.frenchfries); final mediaplayer mpicecream = mediaplayer.create(this, r.raw.icecream); final mediaplayer mphotdog = mediaplayer.create(this, r.raw.hotdog); final mediaplayer mpwine = mediaplayer.create(this, r.raw.wine); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { switch (position) { case 0: mpapple.start(); break; case 1: mpbeer.start(); break; case 2: mpcoffee.start(); break; case 3: mpcheese.start(); break; case 4: mpfrenchfries.start(); break; case 5: mpicecream.start(); break; case 6: mphotdog.start(); break; case 7: mpwine.start(); break; } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_home_english_foods, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
error:
06-04 00:46:21.334 17752-17752/br.gabrielrossetto.tap4learn e/mediaplayer﹕ error (-19,0) 06-04 00:46:29.612 17752-17752/br.gabrielrossetto.tap4learn e/mediaplayer﹕ start called in state 0 06-04 00:46:29.612 17752-17752/br.gabrielrossetto.tap4learn e/mediaplayer﹕ error (-38, 0) 06-04 00:46:29.652 17752-17752/br.gabrielrossetto.tap4learn e/mediaplayer﹕ error (-38,0)
this error means trying call function while in wrong state, example calling seek while media player not ready yet. try add mpapple.setonpreparedlistener()
listener called start()
once prepared, need release player once done recommend in documentation, suggest make 1 mediaplayer
instance, , stop + release before using play new audio file. example
public class homeenglishfoodsactivity extends actionbaractivity { mediaplayer player; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); { gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view int position, long id) { switch (position) { case 0: if(player1 !=null) { player1.stop(); player1.release(); } player1.create(this, r.raw.apple).setonpreparedlistener(new onpreparedlistener() { @override public void onprepared(mediaplayer mediaplayer) { mediaplayer.start(); } }); } } }); } }
Comments
Post a Comment