• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to use MediaPlayer mp.stop(); help

Hello forum!

I have spent hours trying to make a button that can play sound and if i click it again the sound will stop. I have only managed to get the sound playing. If i use mp.stop(); the sound will not start at all...

my source code is:


package AndreasJohnsen.buttonstartstop;


import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class buttonstartstopActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final MediaPlayer mpButtonOn = MediaPlayer.create(this, R.raw.cuttersound);


//CuttersoundButtonLink

Button ButtonOn = (Button) findViewById(R.id.button1);

ButtonOn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub


mpButtonOn.start();




}
});









}
}



reegards Andreas Johnsen :eek:
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones