gpt4 book ai didi

android - 如何停止或释放 MediaPlayer?

转载 作者:行者123 更新时间:2023-11-29 02:09:20 24 4
gpt4 key购买 nike

我正在使用 MediaPlayer 播放 Mp3 文件,但问题是 MediaPlayer 没有在单击后退按钮事件时停止。以下是我的代码。

Java 文件:-

public class Menu_Screen_Activity extends Activity implements OnClickListener {

Button mBtnPlay, mBtnCredits, mBtnHowto, mBtnOptions;
MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_screen);
System.out.println("onCreate Called");
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.mainthememusic_menuscreen);
mp.start();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == event.KEYCODE_BACK) {
System.out.println("Back is called...");
mp.stop();
mp.release();
mp = null;
finish();
}
return super.onKeyDown(keyCode, event);
}
}

最佳答案

添加 setVolumeControlStream(AudioManager.STREAM_MUSIC);

 @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_screen);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
System.out.println("onCreate Called");
mp = new MediaPlayer();
mp = MediaPlayer.create(this, R.raw.mainthememusic_menuscreen);
mp.start();
}

关于android - 如何停止或释放 MediaPlayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8383476/

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