/** The audio stream for phone calls */ publicstaticfinalint STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; /** The audio stream for system sounds */ publicstaticfinalint STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; /** The audio stream for the phone ring */ publicstaticfinalint STREAM_RING = AudioSystem.STREAM_RING; /** The audio stream for music playback */ publicstaticfinalint STREAM_MUSIC = AudioSystem.STREAM_MUSIC; /** The audio stream for alarms */ publicstaticfinalint STREAM_ALARM = AudioSystem.STREAM_ALARM; /** The audio stream for notifications */ publicstaticfinalint STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; /** @hide The audio stream for phone calls when connected to bluetooth */ publicstaticfinalint STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO; /** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */ publicstaticfinalint STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED; /** The audio stream for DTMF Tones */ publicstaticfinalint STREAM_DTMF = AudioSystem.STREAM_DTMF; /** @hide The audio stream for text to speech (TTS) */ publicstaticfinalint STREAM_TTS = AudioSystem.STREAM_TTS;
/** * @hide * Used to indicate no audio focus has been gained or lost. */ publicstaticfinalint AUDIOFOCUS_NONE = 0;
/** * Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) */ publicstaticfinalint AUDIOFOCUS_GAIN = 1; /** * Used to indicate a temporary gain or request of audio focus, anticipated to last a short * amount of time. Examples of temporary changes are the playback of driving directions, or an * event notification. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) */ publicstaticfinalint AUDIOFOCUS_GAIN_TRANSIENT = 2; /** * Used to indicate a temporary request of audio focus, anticipated to last a short * amount of time, and where it is acceptable for other audio applications to keep playing * after having lowered their output level (also referred to as "ducking"). * Examples of temporary changes are the playback of driving directions where playback of music * in the background is acceptable. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) */ publicstaticfinalint AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3; /** * Used to indicate a temporary request of audio focus, anticipated to last a short * amount of time, during which no other applications, or system components, should play * anything. Examples of exclusive and transient audio focus requests are voice * memo recording and speech recognition, during which the system shouldn't play any * notifications, and media playback should have paused. * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) */ publicstaticfinalint AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4; /** * Used to indicate a loss of audio focus of unknown duration. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) */ publicstaticfinalint AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN; /** * Used to indicate a transient loss of audio focus. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) */ publicstaticfinalint AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT; /** * Used to indicate a transient loss of audio focus where the loser of the audio focus can * lower its output volume if it wants to continue playing (also referred to as "ducking"), as * the new focus owner doesn't require others to be silent. * @see OnAudioFocusChangeListener#onAudioFocusChange(int) */ publicstaticfinalint AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
requestAudioFocus 做了什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicintrequestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint){ int status = AUDIOFOCUS_REQUEST_FAILED;
try { // status is guaranteed to be either AUDIOFOCUS_REQUEST_FAILED or // AUDIOFOCUS_REQUEST_GRANTED as focus is requested without the // AUDIOFOCUS_FLAG_DELAY_OK flag status = requestAudioFocus(l, new AudioAttributes.Builder() .setInternalLegacyStreamType(streamType).build(), durationHint, 0/* flags, legacy behavior */); } catch (IllegalArgumentException e) { Log.e(TAG, "Audio focus request denied due to ", e); }