本文介绍了按下后退按钮时如何停止播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,这是我的代码:
final ImageView imageView = (ImageView) findViewById(R.id.Image7);
imageView.setImageResource(mFullSizeIds[position]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(Audio.this,mAudio[position]);
mp.start();
}
});
现在,当用户点击图像时,此代码可以很好地播放音频,但是,当我想退出此活动并转到其他活动时,即使我按了Home键,音频仍在播放音轨还在玩.如何防止这种情况发生?当用户再次轻按图像或按下后退/主页"按钮时,是否可以停止播放曲目?
Now this code works fine to play the audio when the user taps the image, however when I want to exit this activity and go to a different activity, the audio track is still playing, even when I press the home key the audio is still playing. How can I prevent this from happening?Is there a way to stop the track from playing when the user taps the image again or presses the back/home button?
推荐答案
添加
@Override
public void onBackPressed ()
{
if (mp != null)
mp.stop();
super.onBackPressed();
}
和
@Override
public void onPause ()
{
if (mp != null)
{
mp.pause();
mp.stop();
}
super.onPause();
}
这篇关于按下后退按钮时如何停止播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!