gpt4 book ai didi

android - onDestroy 媒体播放器释放

转载 作者:行者123 更新时间:2023-11-30 03:11:28 26 4
gpt4 key购买 nike

我希望当我点击手机上的后退按钮时关闭当前 Activity 并返回菜单并停止媒体播放器
但我收到错误消息:不幸的是武器已停止!单击后退按钮
那么如何解决呢?

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();
}
}

日志:

enter image description here

最佳答案

1) I want when I click on back button on the phone to close current activity and get back to menu and stop Media Player

1) 菜单是什么意思?我的意思是应用程序菜单或设备菜单或通知栏等。

2) But I get error: Unfortunately Weapons has stopped! upon clicking on back button So how to fix that?

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();
}
}
});
}

关于android - onDestroy 媒体播放器释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com