I got three questions.
How can I do it that the sound of my MediaPlayer stops when i switch/close the App/Activity?
How can I do it that sound1 stops when I press on sound2?
How can I stop and play the sound on one button? I mean that when I press button1 once then sound starts and when I press him again the sound stops, and so on.
I already tried it but it doesn't worked.
Here is my MainActivity:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1;
Button button2;
boolean w;
boolean b;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2= (Button) findViewById(R.id.button2);
w = true;
b = true;
//Button1
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(b == true){
mp= MediaPlayer.create(MainActivity.this, R.raw.song1);
mp.start();
b = false;
}else{
mp.stop();
b = true;
}
}
});
//Button 2
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(w == true){
mp= MediaPlayer.create(MainActivity.this, R.raw.sound2);
mp.start();
w = false;
}else{
mp.stop();
w = true;
}}
});
}
//Stopping the sound when switching or closing the App
@Override
public void onPause() {
mp.stop();
super.onPause();
}
}
Can you tell me how to solve my problems?
I'm new on Android Studio and sorry for my bad English :)