/////// First Activity ///////////
public class MainActivity extends AppCompatActivity
{
Button button;Button button3;MediaPlayer mp2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button=(Button)findViewById(R.id.button);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.happy);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent inent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(inent);
if(mp2.isPlaying() == true)
{
mp2.pause();}
else{
mp2.start();}
}
});
}
}
/////////第二项活动
public class Main2Activity extends AppCompatActivity
{
Button button2;MediaPlayer mp2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
答案 0 :(得分:1)
在onPause()
试试这个
mp2.stop();
答案 1 :(得分:0)
根据你问题的前半部分:如果在onPause()和onDestroy()中调用stopMediaPlayer(),你应该得到你想要的东西。
@Override
protected void onPause() {
super.onPause();
stopMediaPlayer();
}
或者你也可以试试这个,
@Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
答案 2 :(得分:0)
在onStop()
和onDestroy()
生命周期方法中暂停媒体播放器。
@Override
protected void onStop() {
super.onPause();
mp2.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp2.pause();
}