@@ -33,18 +33,27 @@ public enum InterruptionSource {
33
33
private InterruptionSource interruptionSource ;
34
34
private Context context ;
35
35
36
+ private Integer focusUsageType ; // AudioAttributes.USAGE_*
37
+ private Integer focusContentType ; // AudioAttributes.CONTENT_TYPE_*
38
+
36
39
public interface AudioFocusChangeListener {
37
40
void onInterruptionStart ();
38
41
void onInterruptionEnd ();
39
42
}
40
43
41
44
public AudioFocusManager (Context context ) {
42
- this (context , InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY );
45
+ this (context , InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY , null , null );
43
46
}
44
47
45
48
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 ) {
46
53
this .context = context ;
47
54
this .interruptionSource = interruptionSource ;
55
+ this .focusUsageType = usageType ;
56
+ this .focusContentType = contentType ;
48
57
49
58
if (interruptionSource == InterruptionSource .AUDIO_FOCUS_ONLY ||
50
59
interruptionSource == InterruptionSource .AUDIO_FOCUS_AND_TELEPHONY ) {
@@ -117,8 +126,8 @@ private void requestAudioFocusInternal() {
117
126
118
127
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
119
128
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 )
122
131
.build ();
123
132
124
133
audioFocusRequest = new AudioFocusRequest .Builder (AudioManager .AUDIOFOCUS_GAIN )
@@ -128,12 +137,47 @@ private void requestAudioFocusInternal() {
128
137
129
138
audioManager .requestAudioFocus (audioFocusRequest );
130
139
} else {
140
+ int streamType = inferPreOStreamType (focusUsageType , focusContentType );
131
141
audioManager .requestAudioFocus (onAudioFocusChangeListener ,
132
- AudioManager . STREAM_VOICE_CALL ,
142
+ streamType ,
133
143
AudioManager .AUDIOFOCUS_GAIN );
134
144
}
135
145
}
136
146
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
+
137
181
private void registerTelephonyListener () {
138
182
if (telephonyManager == null ) {
139
183
Log .w (TAG , "TelephonyManager is null, cannot register telephony listener" );
0 commit comments