@@ -14,6 +14,7 @@ export enum AudioFocusDurationHint {
14
14
15
15
export class TNSPlayer implements TNSPlayerI {
16
16
private _mediaPlayer : android . media . MediaPlayer ;
17
+ private _audioFocusRequest : android . media . AudioFocusRequest ;
17
18
private _mAudioFocusGranted : boolean = false ;
18
19
private _lastPlayerVolume ; // ref to the last volume setting so we can reset after ducking
19
20
private _events : Observable ;
@@ -304,21 +305,37 @@ export class TNSPlayer implements TNSPlayerI {
304
305
* Helper method to ensure audio focus.
305
306
*/
306
307
private _requestAudioFocus ( ) : boolean {
307
- // If it does not enter the codition block, means that we already
308
+ // If it does not enter the condition block, means that we already
308
309
// have focus. Therefore we have to start with `true`.
309
310
let result = true ;
311
+ let focusResult = null ;
310
312
if ( ! this . _mAudioFocusGranted ) {
311
313
const ctx = this . _getAndroidContext ( ) ;
312
314
const am = ctx . getSystemService (
313
315
android . content . Context . AUDIO_SERVICE
314
316
) as android . media . AudioManager ;
315
- // Request audio focus for play back
316
- const focusResult = am . requestAudioFocus (
317
- this . _mOnAudioFocusChangeListener ,
318
- android . media . AudioManager . STREAM_MUSIC ,
319
- this . _durationHint
320
- ) ;
321
317
318
+ // Request audio focus for play back
319
+ if ( android . os . Build . VERSION . SDK_INT >= android . os . Build . VERSION_CODES . O ) {
320
+ const playbackAttributes = new android . media . AudioAttributes . Builder ( )
321
+ . setUsage ( android . media . AudioAttributes . USAGE_MEDIA )
322
+ . setContentType ( android . media . AudioAttributes . CONTENT_TYPE_MUSIC )
323
+ . build ( ) ;
324
+
325
+ this . _audioFocusRequest = new android . media . AudioFocusRequest . Builder ( android . media . AudioManager . AUDIOFOCUS_GAIN )
326
+ . setAudioAttributes ( playbackAttributes )
327
+ . setAcceptsDelayedFocusGain ( true )
328
+ . setOnAudioFocusChangeListener ( this . _mOnAudioFocusChangeListener )
329
+ . build ( ) ;
330
+ focusResult = am . requestAudioFocus ( this . _audioFocusRequest ) ;
331
+ } else {
332
+ focusResult = am . requestAudioFocus (
333
+ this . _mOnAudioFocusChangeListener ,
334
+ android . media . AudioManager . STREAM_MUSIC ,
335
+ this . _durationHint
336
+ ) ;
337
+ }
338
+
322
339
if (
323
340
focusResult === android . media . AudioManager . AUDIOFOCUS_REQUEST_GRANTED
324
341
) {
@@ -327,13 +344,22 @@ export class TNSPlayer implements TNSPlayerI {
327
344
result = false ;
328
345
}
329
346
}
347
+
330
348
return result ;
331
349
}
332
350
333
351
private _abandonAudioFocus ( preserveMP : boolean = false ) : void {
334
352
const ctx = this . _getAndroidContext ( ) ;
335
353
const am = ctx . getSystemService ( android . content . Context . AUDIO_SERVICE ) ;
336
- const result = am . abandonAudioFocus ( this . _mOnAudioFocusChangeListener ) ;
354
+ let result = null ;
355
+
356
+ if ( android . os . Build . VERSION . SDK_INT >= android . os . Build . VERSION_CODES . O ) {
357
+ result = am . abandonAudioFocusRequest ( this . _audioFocusRequest ) ;
358
+ this . _audioFocusRequest = null ;
359
+ } else {
360
+ result = am . abandonAudioFocus ( this . _mOnAudioFocusChangeListener ) ;
361
+ }
362
+
337
363
// Normally we will preserve the MediaPlayer only when pausing
338
364
if ( this . _mediaPlayer && ! preserveMP ) {
339
365
this . _mediaPlayer . release ( ) ;
0 commit comments