android - How to change button text when audio playback is complete? -


i beginner in android studio, , made button plays , stops sound (labeled start/stop) after sound play complete, button doesn't show start text again.

how can make button change text start when playback done?

how can make sound stop when switching view?

my code:

public class insideaventador extends activity {     mediaplayer mysound;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.insideaventador);         mysound = mediaplayer.create(this, r.raw.lamborghini);     }       public void startup(view v) {         button button = (button) v;         if (mysound == null) {             mysound = mediaplayer.create(insideaventador.this, r.raw.lamborghini);         }          if (mysound.isplaying()) {             mysound.pause();             ((button) v).settext("start");         } else {             mysound.start();             ((button) v).settext("stop");         }      }  } 

mediaplayer provides oncompletionlistener interface can implement notified when playback of audio file ended. can set listener via setoncompletionlistener().

eg.

mysound.setoncompletionlistener(new mediaplayer.oncompletionlistener(){     public void oncompletion(mediaplayer mp){         // perform steps here, eg.         ((button) v).settext("start");         // (make sure make v final before)     } }) 

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 -