I'm programming a media player with android studio. It has 3 option: 1.play 2.stop 3.pause... the play and stop works. But when I click on pause it doesn't work and after that the stop not works too and the music play continuously! it's the code on MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1= (Button) findViewById(R.id.play);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,PlayAndPause.class);
startService(i);
a=1;
}
});
Button b2 = (Button) findViewById(R.id.stop);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(MainActivity.this,PlayAndPause.class);
stopService(i);
}
});
Button b3= (Button) findViewById(R.id.pause);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,PlayAndPause.class);
startService(i);
a=2;
}
});
}
and it's the PlayandPause service:
public class PlayAndPause extends Service {
MediaPlayer mp;
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mp=MediaPlayer.create(this,R.raw.s);
if(MainActivity.a==1)
mp.start();
if(MainActivity.a==2) {
mp.pause();
}
}
@Override
public void onDestroy() {
super.onDestroy();
mp.stop();
}
please help! thanks.