@@ -33,18 +33,27 @@ public enum InterruptionSource {
3333 private InterruptionSource interruptionSource ;
3434 private Context context ;
3535
36+ private Integer focusUsageType ; // AudioAttributes.USAGE_*
37+ private Integer focusContentType ; // AudioAttributes.CONTENT_TYPE_*
38+
3639 public interface AudioFocusChangeListener {
3740 void onInterruptionStart ();
3841 void onInterruptionEnd ();
3942 }
4043
4144 public AudioFocusManager (Context context ) {
42- this (context , InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY );
45+ this (context , InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY , null , null );
4346 }
4447
4548 public AudioFocusManager (Context context , InterruptionSource interruptionSource ) {
49+ this (context , interruptionSource , null , null );
50+ }
51+
52+ public AudioFocusManager (Context context , InterruptionSource interruptionSource , Integer usageType , Integer contentType ) {
4653 this .context = context ;
4754 this .interruptionSource = interruptionSource ;
55+ this .focusUsageType = usageType ;
56+ this .focusContentType = contentType ;
4857
4958 if (interruptionSource == InterruptionSource .AUDIO_FOCUS_ONLY ||
5059 interruptionSource == InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY ) {
@@ -117,8 +126,8 @@ private void requestAudioFocusInternal() {
117126
118127 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
119128 AudioAttributes audioAttributes = new AudioAttributes .Builder ()
120- .setUsage (AudioAttributes .USAGE_VOICE_COMMUNICATION )
121- .setContentType (AudioAttributes .CONTENT_TYPE_SPEECH )
129+ .setUsage (focusUsageType != null ? focusUsageType : AudioAttributes .USAGE_VOICE_COMMUNICATION )
130+ .setContentType (focusContentType != null ? focusContentType : AudioAttributes .CONTENT_TYPE_SPEECH )
122131 .build ();
123132
124133 audioFocusRequest = new AudioFocusRequest .Builder (AudioManager .AUDIOFOCUS_GAIN )
@@ -128,12 +137,47 @@ private void requestAudioFocusInternal() {
128137
129138 audioManager .requestAudioFocus (audioFocusRequest );
130139 } else {
140+ int streamType = inferPreOStreamType (focusUsageType , focusContentType );
131141 audioManager .requestAudioFocus (onAudioFocusChangeListener ,
132- AudioManager . STREAM_VOICE_CALL ,
142+ streamType ,
133143 AudioManager .AUDIOFOCUS_GAIN );
134144 }
135145 }
136146
147+ private int inferPreOStreamType (Integer usageType , Integer contentType ) {
148+ if (usageType != null ) {
149+ if (usageType == AudioAttributes .USAGE_MEDIA
150+ || usageType == AudioAttributes .USAGE_GAME
151+ || usageType == AudioAttributes .USAGE_ASSISTANT ) {
152+ return AudioManager .STREAM_MUSIC ;
153+ }
154+ if (usageType == AudioAttributes .USAGE_VOICE_COMMUNICATION
155+ || usageType == AudioAttributes .USAGE_VOICE_COMMUNICATION_SIGNALLING ) {
156+ return AudioManager .STREAM_VOICE_CALL ;
157+ }
158+ if (usageType == AudioAttributes .USAGE_NOTIFICATION
159+ || usageType == AudioAttributes .USAGE_NOTIFICATION_RINGTONE
160+ || usageType == AudioAttributes .USAGE_NOTIFICATION_COMMUNICATION_REQUEST ) {
161+ return AudioManager .STREAM_NOTIFICATION ;
162+ }
163+ if (usageType == AudioAttributes .USAGE_ALARM ) {
164+ return AudioManager .STREAM_ALARM ;
165+ }
166+ }
167+
168+ if (contentType != null ) {
169+ if (contentType == AudioAttributes .CONTENT_TYPE_MUSIC
170+ || contentType == AudioAttributes .CONTENT_TYPE_MOVIE ) {
171+ return AudioManager .STREAM_MUSIC ;
172+ }
173+ if (contentType == AudioAttributes .CONTENT_TYPE_SPEECH ) {
174+ return AudioManager .STREAM_VOICE_CALL ;
175+ }
176+ }
177+
178+ return AudioManager .STREAM_VOICE_CALL ;
179+ }
180+
137181 private void registerTelephonyListener () {
138182 if (telephonyManager == null ) {
139183 Log .w (TAG , "TelephonyManager is null, cannot register telephony listener" );
0 commit comments