audio - Action when sound stops? Android Studio -


i'm making first app in android studio. got activity, plays sound mediaplayer, , supposed return main activity, either button (works fine) or when sound stops(what i'm missing). there easy way check if sound has stopped playing, , let trigger action?

here code far:

import android.content.intent; import android.media.mediaplayer; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.imagebutton;   public class image_button_2 extends actionbaractivity implements view.onclicklistener {  imagebutton imagebutton6; mediaplayer mysound;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_image_button_2);     imagebutton6 = (imagebutton) findviewbyid(r.id.imagebutton6);     imagebutton6.setonclicklistener(this);     mysound = mediaplayer.create(this, r.raw.hb);     mysound.start(); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_image_button_2, 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); }  private void imagebutton6click(){      mysound.stop();     intent intent = new intent(getapplicationcontext(), mainactivity.class);     intent.setflags(intent.flag_activity_clear_top);     startactivity(intent);  }  @override public void onclick(view v) {      switch (v.getid()){         case r.id.imagebutton6:             imagebutton6click();             break;     }  } 

}

try out , take mainactivity when music finished

 mysound.setoncompletionlistener(new mediaplayer.oncompletionlistener() {         public void oncompletion(mediaplayer mp) {             imagebutton6click();         }     }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -