Android-Android中怎么播放背景音乐而不会中断?

Android-Android中怎么播放背景音乐而不会中断?

夜无邪 发布于 2017-05-11 字数 1226 浏览 1148 回复 2

我现在需要给应用播放一个背景音乐,我使用了mediaplayer这个类来播放,但是在应用中我还有其他的地方需要播放音效,一播放其他音效,背景音乐就终止了(其他音效我是放其他类里面的,不用同一个对象,貌似还是这样),我想问下这个要怎么解决?希望能尽量详细点,开发经验少,很多地方都不懂的说,谢谢
贴个代码:

public Boolean playMp3()
{
MediaPlayer mp = MediaPlayer.create(myContext,
R.raw.background_gongyuan);
OnCompletionListener listenerCompletion = new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
p++;
if (p >= 1)
{
p = 0;
return;
}
playMp3();
}
};
mp.setOnCompletionListener(listenerCompletion);
mp.setLooping(false);
try
{
mp.seekTo(0);
mp.start();
return true;
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
return false;
}

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2017-10-19 2 楼

我刚开始写的时候 总是发生 怎么同一个音乐 有好几在播放啊
如果你吧 mediaplayer 当成一个音乐播放者, 每一个mediaplayer 只能同时播放一个音频文件.
那你 new 一个mediaplayer 播放背景音, 在new 一个播放另一种特效音 应该就可以了
顶多在复杂一点的就是放在不同thread 里面new 不过我感觉应该没有这个必要.

瑾兮 2017-09-04 1 楼

Android中有个AudioFocus的概念,你可以为Music去监听AudioFocus。
这个问题一下子说不清楚,建议先看下http://developer.android.com/training/managing-audio/audio-focus.html,这里面介绍了一些Audio focus的基本概念。希望对你有帮助!

补充一下:

(1)在Call Play之前要获取Audio Focus

mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

(2)具体AudioFocus Listener的实现

private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
// AudioFocus is a new feature: focus updates are made verbose on purpose
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_LOSS");
if(mLocalPlayer.isPlaying()) {
mLocalPlayer.pause();
}
if (mMediaplayerHandler != null && mMediaplayerHandler.hasMessages(FADEIN)) {
mMediaplayerHandler.removeMessages(FADEIN);
mPlayer.setVolume(1.0f);
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
if(mLocalPlayer.isPlaying()) {
mLocalPlayer.pause();
}

if (mMediaplayerHandler != null && mMediaplayerHandler.hasMessages(FADEIN)) {
mMediaplayerHandler.removeMessages(FADEIN);
mPlayer.setVolume(1.0f);
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
if(mLocalPlayer.isPlaying()) {
mPlayer.setVolume(0.0f);
Log.d(TAG, "Mute since received AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.v(TAG, "AudioFocus: received AUDIOFOCUS_GAIN");
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
boolean isPhoneHook = false;
if (tm != null) {
isPhoneHook = (tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK);
}
if(!mLocalPlayer.isPlaying() && mPausedByTransientLossOfFocus) {
mPausedByTransientLossOfFocus = false;
if (!isPhoneHook) {
startAndFadeIn();
}
} else if (mMuteByTransientLossOfFocusCanDuck) {
mMuteByTransientLossOfFocusCanDuck = false;
if (!isPhoneHook) {
mPlayer.setVolume(1.0f);
}
Log.d(TAG, "Unmute since received AUDIOFOCUS_GAIN");
}
break;
default:
Log.e(TAG, "Unknown audio focus change code");
}
}
};

(3)在Activity OnDestory的时候释放AudioFocus.

mAudioManager.abandonAudioFocus(mAudioFocusListener);

(4)这只是一个简单的Main Thread实现的例子,真正的做Music是要在Service里面实现的,你自己试试看吧