requestaudiofocus 的正确参数是什么?
What are the correct arguments for requestaudiofocus?
我是 Android 和 Java 的新手。我一直在使用 Android 开发人员和其他网站提供的 MediaPlayer 和 AudioManager 示例。
我注意到,对于 requestAudioFocus() 的调用似乎使用了两个单独的签名。
例如,来自 http://developer.android.com/guide/topics/media/mediaplayer.html
网站有:
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.
}
加上以下文字:
"The first parameter to requestAudioFocus() is an AudioManager.OnAudioFocusChangeListener, whose onAudioFocusChange() method is called whenever there is a change in audio focus. Therefore, you should also implement this interface on your service and activities. For example:"
(使用以下代码:)
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"。
如果您能解释为什么使用 "this" 而不是 AudioManager.OnAudioFocusChangeListener 作为参数,我们将不胜感激。
它总是需要一个 onAudioFocusedChangeListener。在传递这个的情况下,当前class实现了onAudioFocusChangeListener接口。
我是 Android 和 Java 的新手。我一直在使用 Android 开发人员和其他网站提供的 MediaPlayer 和 AudioManager 示例。
我注意到,对于 requestAudioFocus() 的调用似乎使用了两个单独的签名。 例如,来自 http://developer.android.com/guide/topics/media/mediaplayer.html 网站有:
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.
}
加上以下文字:
"The first parameter to requestAudioFocus() is an AudioManager.OnAudioFocusChangeListener, whose onAudioFocusChange() method is called whenever there is a change in audio focus. Therefore, you should also implement this interface on your service and activities. For example:" (使用以下代码:)
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"。
如果您能解释为什么使用 "this" 而不是 AudioManager.OnAudioFocusChangeListener 作为参数,我们将不胜感激。
它总是需要一个 onAudioFocusedChangeListener。在传递这个的情况下,当前class实现了onAudioFocusChangeListener接口。