Android:如何停止播放音乐?
我创建了一个应用程序和一些背景音乐。
结果 = 成功
问题是.. -.- 如果应用程序收到呼叫,音乐仍在播放并且播放而不是用户的原始铃声。 另外,如果在手机上选择了“主页”按钮。活动退出但音乐仍在运行?
如果选择手机上的“返回”按钮,我设法退出音乐。
除此之外,音乐 24/7 运行。
我用过的是.
应用程序中的菜单选项,当用户选择“菜单”时,会弹出选项列表以及选择“设置...”的选项 如果用户选择“设置...”,则会弹出首选项对话框以选中或取消选中音乐(开/关) 结果 = 成功
音乐在整个应用程序中从一个活动到另一个活动播放。
这是我的应用程序中的媒体播放器代码。
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.bgmusic);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
finish();
Music.stop(this);
return true;
}
return super.onKeyDown(keyCode, event);
}
这是我的 music.java
public class Music {
private static MediaPlayer mp = null;
/** Stop old song and start new one */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
/** Stop the music */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.pause();
mp.release();
mp = null;
}
}
}
有人知道吗?
I have created an application and with some background music.
Result = SUCCESS
Problem is.. -.- if app receives a call, music is still playing and plays instead of the user's original ringtone.
Plus if "Home" button is selected on the phone. Activity exits but the music is still running?
I managed to quit the music if the "Back" button on the phone is selected.
other than that music runs 24/7.
What I have used is.
A menu option in the application that when the user selected "Menu" a list of options pop up and an option to choose "Settings..."
If the user selects "Settings..." a preference dialogue pops up to check or uncheck music (ON/OFF)
Result = SUCCESS
The music plays throughout the application, from activity to activity.
This is my media player code in my application.
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.bgmusic);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
finish();
Music.stop(this);
return true;
}
return super.onKeyDown(keyCode, event);
}
This is my music.java
public class Music {
private static MediaPlayer mp = null;
/** Stop old song and start new one */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
/** Stop the music */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.pause();
mp.release();
mp = null;
}
}
}
anyone have a clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要调用
onPause 方法。当从一项活动切换到另一项活动时,这可能会立即出现故障。
另请阅读此问题和已接受的答案。
You need to call
in the onPause method. This may have the effect of an instant glitch, when switching from one of yours activities to another.
Read also this question and the accepted answer.