我希望当我点击手机上的后退按钮关闭当前活动并返回菜单并停止媒体播放器
时
但我得到错误:Unfortunately Weapons has stopped! upon clicking on back button
那么如何解决这个问题?
public class pushke extends Activity {
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.guns);
initControls();
final MediaPlayer mp=MediaPlayer.create(this, R.raw.uzi);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}
});
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{ }
@Override
public void onStartTrackingTouch(SeekBar arg0)
{ }
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
}
logcat的:
答案 0 :(得分:1)
1)当我点击手机上的后退按钮关闭电流时我想要 活动并返回菜单并停止媒体播放器
1)菜单是什么意思?我的意思是应用程序菜单或设备菜单或通知栏等。
2)但我得到错误:不幸的是武器已经停止了!点击后 后退按钮那么如何解决这个问题?
2)
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mp!=null){
mp.stop();
mp.release();
mp = null;
}
}
编辑:
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.guns);
initControls();
mp=MediaPlayer.create(this, R.raw.uzi);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}
});
}
答案 1 :(得分:0)
在释放媒体播放器之前的On destroy中,请停止媒体播放器