1

I am a beginner in Android Studio, and I made a button which plays and stops a sound (labeled start/stop) but after the sound play is complete, the button doesn't show the start text again.

How can I make the button change its text to start when playback is done?

How can I make the sound stop when switching to another 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");
        }

    }

}
MasterAM
  • 16,283
  • 6
  • 45
  • 66
beginner
  • 33
  • 6

4 Answers4

0

MediaPlayer provides an OnCompletionListener interface that you can implement to get notified when the playback of your audio file ended. You can then set the listener via setOnCompletionListener().

Eg.

mysound.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
    public void onCompletion(MediaPlayer mp){
        // Perform your steps here, eg.
        ((Button) v).setText("START");
        // (Make sure you make v final before)
    }
})
FD_
  • 12,947
  • 4
  • 35
  • 62
  • //perform your steps here ,sorry but from which line?? – beginner Jul 14 '15 at 11:21
  • I'm sorry but I don't understand your question. Do you mean where you should insert these lines of code? – FD_ Jul 14 '15 at 11:22
  • yup ,this is exactly what i mean.. :D – beginner Jul 14 '15 at 11:23
  • Without adding a new method, you could add it to the end of startup(). I'd recommend calling it only once after the Activity was created, but that would require you to extract the button view via `findViewById()` once there. – FD_ Jul 14 '15 at 11:26
  • thanks a lot for your,reply ,you helped me a lot ,but another question please, how to make the button stick to its place in the layout and doesn't change with the size of the screen? – beginner Jul 14 '15 at 11:51
  • If my answer helped for this specific questions, please accept it by clicking the checkmark on the left. If you have another question, please open a new question so people can answer it properly. – FD_ Jul 14 '15 at 13:44
0

you need to reset and prepare as well

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.reset();
        mysound.prepare(); 
        mysound.start(); 
        ((Button) v).setText("STOP");
    } 

} 
0

Hear you can get answer for your first question...

https://stackoverflow.com/a/15635785/4156278

and when choosing another layout or page you can stop playing sound in onPause().

Community
  • 1
  • 1
0
public class MainActivity extends Activity {

    Context context = this;
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mp = MediaPlayer.create(context, R.raw.tone);
        final Button b = (Button) findViewById(R.id.Button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (mp.isPlaying()){
                        mp.pause();
                        b.setText("Play");
                    }
                  else{
                       mp.start();
                b.setText("Pause");
                    }
            }
        });

    }

}
Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Scott Weldon Jun 16 '16 at 22:53