如何语域我MusicPlayer应用到OnAudioFocusChangeListener [英] how to resgister my MusicPlayer App to OnAudioFocusChangeListener
问题描述
我创建一个音乐播放器应用程序。
I am creating a music player app.
我想:
- 当用户使用我的应用程序播放一首歌曲,那么默认的音乐播放器应该得到停止(播放)
- 如果其他一些应用程序启动时打我的应用程序已在播放,然后我的应该得到停止。
我能够使用音频聚焦。
但对于第二点,即处理 AUDIOFOCUS_LOSS
,我面临的一些问题。
I am able to work out with the 1st point using Audio Focus.
But for the 2nd point, i.e. Handling AUDIOFOCUS_LOSS
, i'm facing some issues.
问题:
我跟着文档,但我应该怎么注册我的音乐播放器服务来检测AudioFocus已丢失。
Problem: I've followed the documentation but how should I register my music player service to detect that AudioFocus has been lost.
对于第一个点,这里是code:
if (reqAudioFocus()) {
mPlayer.start();
}
private boolean reqAudioFocus() {
boolean gotFocus = false;
int audioFocus = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (audioFocus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
gotFocus = true;
} else {
gotFocus = false;
}
return gotFocus;
}
对于第二点我的code迄今:
//Class implements OnAudioFocusChangeListener
@Override
public void onAudioFocusChange(int focusChange) {
/*
* TODO : Call stopService/onDestroy() method. Pause the track; Change
* the Play/Pause icon; save the track postion for seekTo() method.
*/
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
// TODO: Pause playback
if (mPlayer.isPlaying()) {
mPlayer.pause();
}
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// TODO: Resume Playback
if (!mPlayer.isPlaying()) {
mPlayer.start();
}
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// TODO: stop playback.
// am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
}
}
现在,如何让我的应用程序调用 onAudioFocusChange()
当应用程序失去音频焦点。
Now how to make my application call onAudioFocusChange()
when app loses audio focus.
感谢
推荐答案
的第一个参数<一个href=\"http://developer.android.com/reference/android/media/AudioManager.html#requestAudioFocus%28android.media.AudioManager.OnAudioFocusChangeListener,%20int,%20int%29\"相对=nofollow> requestAudioFocus
是一个 OnAudioFocusChangeListener
实例。你传递空
的时刻。假设你已经实施 reqAudioFocus
和 onAudioFocusChanged
在同一类,通过这个
而不是空
应该够了。
The first argument to requestAudioFocus
is an OnAudioFocusChangeListener
instance. You're passing null
at the moment. Assuming you've implemented reqAudioFocus
and onAudioFocusChanged
on the same class, passing this
instead of null
should be enough.
这篇关于如何语域我MusicPlayer应用到OnAudioFocusChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!