请求焦点:
/**
* Request audio focus.
* Send a request to obtain the audio focus
* @param l the listener to be notified of audio focus changes
* @param streamType the main audio stream type affected by the focus request
* @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request
* is temporary, and focus will be abandonned shortly. Examples of transient requests are
* for the playback of driving directions, or notifications sounds.
* Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for
* the previous focus owner to keep playing if it ducks its audio output.
* Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request
* that benefits from the system not playing disruptive sounds like notifications, for
* usecases such as voice memo recording, or speech recognition.
* Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such
* as the playback of a song or a video.
* @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
*/
public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint)
下面几个参数都是和onAudioFocusChange(int focusChange)中的focusChange参数对应的
AUDIOFOCUS_GAIN_TRANSIENT:获取的短暂的焦点,就是告知被剥夺者,你很快会重获焦点。对应AUDIOFOCUS_LOSS_TRANSIENT
AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:获取焦点,告知被剥夺者,你可以继续播放并将音量降为0或者低音量播放。对应AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK
AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE:获取短暂焦点,但非常期望没有其他声音在播放。这个没有对应的loss标志。
AUDIOFOCUS_GAIN:获取较长期的焦点,对应AUDIOFOCUS_LOSS;
下面是其他博客的解释,但是我觉得是错误的,AUDIOFOCUS_LOSS不是失去很长时间,而是在request的时候参数是AUDIOFOCUS_GAIN,而AUDIOFOCUS_LOSS的解释是
失去焦点的时间未知,可能会长时间失去焦点。
focusChange参数告你音频焦点时如何改变的,并且能够是下面的值之一(它们是所有的在AudioManager类中定义的常量):
AUDIOFOCUS_GAIN&#