我是Android和Java的新手.我一直在使用Android Developer和其他网站提供的MediaPlayer和AudioManager示例.
我注意到的是,对于requestAudioFocus()的调用,似乎有两个单独的签名被使用.
例如,从@L_404_1@
网站有:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUdio_SERVICE);
int result = audioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUdioFOCUS_GAIN);
if (result != AudioManager.AUdioFOCUS_REQUEST_GRANTED) {
// could not get audio focus.
}
使用以下文本:
“requestAudioFocus()的第一个参数是AudioManager.OnAudioFocusChangeListener,只要音频焦点发生变化,就会调用其onAudioFocusChange()方法.因此,您还应该在服务和活动上实现此接口.例如:”
(使用以下代码:)
class MyService extends Service
implements AudioManager.OnAudioFocusChangeListener {
// ....
public void onAudioFocusChange(int focusChange) {
// Do something based on focus change...
}
}
然后从网站:
http://developer.android.com/training/managing-audio/audio-focus.html
有:
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUdioFOCUS_LOSS_TRANSIENT) {
// Pause playback
} else if (focusChange == AudioManager.AUdioFOCUS_GAIN) {
// Resume playback
} else if (focusChange == AudioManager.AUdioFOCUS_LOSS) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
AudioManager am = mContext.getSystemService(Context.AUdio_SERVICE);
...
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,// Use the music stream.
AudioManager.STREAM_MUSIC,// Request permanent focus.
AudioManager.AUdioFOCUS_GAIN);
if (result == AudioManager.AUdioFOCUS_REQUEST_GRANTED) {
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Start playback.
}
我已经在众多网站上看到了这种二分法,为处理音频焦点的变化提供了示例代码.
我的理解是“this”提供了应用程序当前状态的上下文.
我不明白为什么在某些情况下“this”是正确的参数,而在其他情况下,在调用requestAudioFocus()时,更改侦听器的句柄是正确的参数.
实际上我提供的第一个例子说明第一个参数应该是AudioManager.OnAudioFocusChangeListener.但是使用“这个”.
如果您可以解释为什么使用“this”而不是AudioManager.OnAudioFocusChangeListener用作参数,我们将不胜感激.
最佳答案
原文链接:https://www.f2er.com/android/431193.html