Four Trys to Start / Stop / Start MediaPlayer on Button Click Android Java -
goal: start, stop, start play of sound clicking button 3 times
hello all
i new android.
have had trouble restarting play of sound after stopping click of button.
have researched problem ( link ) , found different methods apply.
so, have tried 4 ways (unsuccessfully) this.
there 4 buttons use different methods.
button 1 used control.
comments results of different methods.
post couple entries logcat below.
thank you
here link relevant android state diagram
src/com.example.playsound/mainactivity.java
package com.example.playsound; import java.io.ioexception; import android.app.activity; import android.content.intent; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.widget.button; public class mainactivity extends activity { //create media player @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //button sound final mediaplayer mplayer1 = mediaplayer.create(mainactivity.this, r.raw.playsound0); final mediaplayer mplayer2 = mediaplayer.create(mainactivity.this, r.raw.playsound1); final mediaplayer mplayer3 = mediaplayer.create(mainactivity.this, r.raw.playsound2); final mediaplayer mplayer4 = mediaplayer.create(mainactivity.this, r.raw.playsound3); //button references button button1 = (button)this.findviewbyid(r.id.button1); button button2 = (button)this.findviewbyid(r.id.button2); button button3 = (button)this.findviewbyid(r.id.button3); button button4 = (button)this.findviewbyid(r.id.button4); //button 1 onclicklistener button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(mplayer1.isplaying() ) { try { mplayer1.stop(); } catch (illegalstateexception e) { e.printstacktrace(); } } // button 1 else { // plays sound upon first click, stops on second click try { // not play on third click mplayer1.start(); } catch (illegalstateexception e) { e.printstacktrace(); } } } }); //button 2 uses .prepareasync() button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(mplayer2.isplaying() ) { // button 2 try { // plays sound upon first click, stops on second click mplayer2.stop(); // not play upon third click mplayer2.reset(); } catch (illegalstateexception e) { e.printstacktrace(); } } else { try { mplayer2.prepareasync(); } catch (illegalstateexception e) { e.printstacktrace(); } mplayer2.start(); } } }); button3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(mplayer3.isplaying() ) { try { // button 3 mplayer3.stop(); // not play sound upon first button click } catch (illegalstateexception e) { e.printstacktrace(); } } else { mplayer3.setonpreparedlistener(new mediaplayer.onpreparedlistener() { @override public void onprepared(mediaplayer mplayer3) { mplayer3.start(); } }); } } }); //button 4 onclicklistener button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(mplayer4.isplaying() ) { try { mplayer4.stop(); } catch (illegalstateexception e) { e.printstacktrace(); } } // button 4 else { // not play sound on first click try { mplayer4.prepare(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } mplayer4.start(); } } }); } } most errors in logcat similar to:
prepareasync called in state 8
start called in state 0
start called in state 1
errors "call in state #" errors.
have referenced diagram posted in link above.
still have these errors when attempt call methods in correct place.
you calling methods in unappropriate states.
prepareasync can called in {initialized, stopped} state.
start can called in {prepared, started, paused, playbackcompleted} states.
so if try call in other states throw exception.
to have clear knowledge states read doc
Comments
Post a Comment