@@ -4,9 +4,12 @@ import { AudioPlayerEvents, AudioPlayerOptions } from '../options';
4
4
5
5
export enum AudioFocusDurationHint {
6
6
AUDIOFOCUS_GAIN = android . media . AudioManager . AUDIOFOCUS_GAIN ,
7
- AUDIOFOCUS_GAIN_TRANSIENT = android . media . AudioManager . AUDIOFOCUS_GAIN_TRANSIENT ,
8
- AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android . media . AudioManager . AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK ,
9
- AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android . media . AudioManager . AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
7
+ AUDIOFOCUS_GAIN_TRANSIENT = android . media . AudioManager
8
+ . AUDIOFOCUS_GAIN_TRANSIENT ,
9
+ AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = android . media . AudioManager
10
+ . AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK ,
11
+ AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = android . media . AudioManager
12
+ . AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
10
13
}
11
14
12
15
export class TNSPlayer implements TNSPlayerI {
@@ -17,7 +20,9 @@ export class TNSPlayer implements TNSPlayerI {
17
20
private _durationHint : AudioFocusDurationHint ;
18
21
private _options : AudioPlayerOptions ;
19
22
20
- constructor ( durationHint : AudioFocusDurationHint = AudioFocusDurationHint . AUDIOFOCUS_GAIN ) {
23
+ constructor (
24
+ durationHint : AudioFocusDurationHint = AudioFocusDurationHint . AUDIOFOCUS_GAIN
25
+ ) {
21
26
this . _durationHint = durationHint ;
22
27
}
23
28
@@ -76,17 +81,15 @@ export class TNSPlayer implements TNSPlayerI {
76
81
options . autoPlay = true ;
77
82
}
78
83
79
- if ( ! this . _player ) {
80
- this . _player = new android . media . MediaPlayer ( ) ;
81
- }
82
-
83
84
// request audio focus, this will setup the onAudioFocusChangeListener
84
85
if ( ! options . audioMixing ) {
85
86
this . _mAudioFocusGranted = this . _requestAudioFocus ( ) ;
86
87
}
87
88
88
89
const audioPath = resolveAudioFilePath ( options . audioFile ) ;
89
- this . _player . setAudioStreamType ( android . media . AudioManager . STREAM_MUSIC ) ;
90
+ this . _player . setAudioStreamType (
91
+ android . media . AudioManager . STREAM_MUSIC
92
+ ) ;
90
93
this . _player . reset ( ) ;
91
94
this . _player . setDataSource ( audioPath ) ;
92
95
@@ -175,12 +178,17 @@ export class TNSPlayer implements TNSPlayerI {
175
178
this . _sendEvent ( AudioPlayerEvents . started ) ;
176
179
// set volume controls
177
180
// https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)
178
- Application . android . foregroundActivity . setVolumeControlStream ( android . media . AudioManager . STREAM_MUSIC ) ;
181
+ Application . android . foregroundActivity . setVolumeControlStream (
182
+ android . media . AudioManager . STREAM_MUSIC
183
+ ) ;
179
184
180
185
// register the receiver so when calls or another app takes main audio focus the player pauses
181
186
Application . android . registerBroadcastReceiver (
182
187
android . media . AudioManager . ACTION_AUDIO_BECOMING_NOISY ,
183
- ( context : android . content . Context , intent : android . content . Intent ) => {
188
+ (
189
+ context : android . content . Context ,
190
+ intent : android . content . Intent
191
+ ) => {
184
192
this . pause ( ) ;
185
193
}
186
194
) ;
@@ -221,13 +229,19 @@ export class TNSPlayer implements TNSPlayerI {
221
229
// this checks on API 23 and up
222
230
if ( android . os . Build . VERSION . SDK_INT >= 23 && this . play ) {
223
231
if ( this . _player ?. isPlaying ( ) ) {
224
- ( this . _player as any ) . setPlaybackParams ( ( this . _player as any ) . getPlaybackParams ( ) . setSpeed ( speed ) ) ;
232
+ ( this . _player as any ) . setPlaybackParams (
233
+ ( this . _player as any ) . getPlaybackParams ( ) . setSpeed ( speed )
234
+ ) ;
225
235
} else {
226
- ( this . _player as any ) . setPlaybackParams ( ( this . _player as any ) . getPlaybackParams ( ) . setSpeed ( speed ) ) ;
236
+ ( this . _player as any ) . setPlaybackParams (
237
+ ( this . _player as any ) . getPlaybackParams ( ) . setSpeed ( speed )
238
+ ) ;
227
239
this . _player ?. pause ( ) ;
228
240
}
229
241
} else {
230
- console . warn ( 'Android device API is not 23+. Cannot set the playbackRate on lower Android APIs.' ) ;
242
+ console . warn (
243
+ 'Android device API is not 23+. Cannot set the playbackRate on lower Android APIs.'
244
+ ) ;
231
245
}
232
246
}
233
247
@@ -241,7 +255,9 @@ export class TNSPlayer implements TNSPlayerI {
241
255
// (Refer to: https://developer.android.com/reference/android/media/MediaPlayer#state-diagram)
242
256
this . _options = undefined ;
243
257
// unregister broadcast receiver
244
- Application . android . unregisterBroadcastReceiver ( android . media . AudioManager . ACTION_AUDIO_BECOMING_NOISY ) ;
258
+ Application . android . unregisterBroadcastReceiver (
259
+ android . media . AudioManager . ACTION_AUDIO_BECOMING_NOISY
260
+ ) ;
245
261
246
262
this . _abandonAudioFocus ( ) ;
247
263
}
@@ -293,15 +309,19 @@ export class TNSPlayer implements TNSPlayerI {
293
309
let result = true ;
294
310
if ( ! this . _mAudioFocusGranted ) {
295
311
const ctx = this . _getAndroidContext ( ) ;
296
- const am = ctx . getSystemService ( android . content . Context . AUDIO_SERVICE ) as android . media . AudioManager ;
312
+ const am = ctx . getSystemService (
313
+ android . content . Context . AUDIO_SERVICE
314
+ ) as android . media . AudioManager ;
297
315
// Request audio focus for play back
298
316
const focusResult = am . requestAudioFocus (
299
317
this . _mOnAudioFocusChangeListener ,
300
318
android . media . AudioManager . STREAM_MUSIC ,
301
319
this . _durationHint
302
320
) ;
303
321
304
- if ( focusResult === android . media . AudioManager . AUDIOFOCUS_REQUEST_GRANTED ) {
322
+ if (
323
+ focusResult === android . media . AudioManager . AUDIOFOCUS_REQUEST_GRANTED
324
+ ) {
305
325
result = true ;
306
326
} else {
307
327
result = false ;
@@ -388,36 +408,40 @@ export class TNSPlayer implements TNSPlayerI {
388
408
return this . _mediaPlayer ;
389
409
}
390
410
391
- private _mOnAudioFocusChangeListener = new android . media . AudioManager . OnAudioFocusChangeListener ( {
392
- onAudioFocusChange : ( focusChange : number ) => {
393
- switch ( focusChange ) {
394
- case android . media . AudioManager . AUDIOFOCUS_GAIN :
395
- // Set volume level to desired levels
396
- // if last volume more than 10 just set to 1.0 float
397
- if ( this . _lastPlayerVolume && this . _lastPlayerVolume >= 10 ) {
398
- this . volume = 1.0 ;
399
- } else if ( this . _lastPlayerVolume ) {
400
- this . volume = parseFloat ( '0.' + this . _lastPlayerVolume . toString ( ) ) ;
401
- }
411
+ private _mOnAudioFocusChangeListener = new android . media . AudioManager . OnAudioFocusChangeListener (
412
+ {
413
+ onAudioFocusChange : ( focusChange : number ) => {
414
+ switch ( focusChange ) {
415
+ case android . media . AudioManager . AUDIOFOCUS_GAIN :
416
+ // Set volume level to desired levels
417
+ // if last volume more than 10 just set to 1.0 float
418
+ if ( this . _lastPlayerVolume && this . _lastPlayerVolume >= 10 ) {
419
+ this . volume = 1.0 ;
420
+ } else if ( this . _lastPlayerVolume ) {
421
+ this . volume = parseFloat (
422
+ '0.' + this . _lastPlayerVolume . toString ( )
423
+ ) ;
424
+ }
402
425
403
- this . resume ( ) ;
404
- break ;
405
- case android . media . AudioManager . AUDIOFOCUS_GAIN_TRANSIENT :
406
- // You have audio focus for a short time
407
- break ;
408
- case android . media . AudioManager . AUDIOFOCUS_LOSS :
409
- this . pause ( ) ;
410
- break ;
411
- case android . media . AudioManager . AUDIOFOCUS_LOSS_TRANSIENT :
412
- // Temporary loss of audio focus - expect to get it back - you can keep your resources around
413
- this . pause ( ) ;
414
- break ;
415
- case android . media . AudioManager . AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK :
416
- // Lower the volume, keep playing
417
- this . _lastPlayerVolume = this . volume ;
418
- this . volume = 0.2 ;
419
- break ;
426
+ this . resume ( ) ;
427
+ break ;
428
+ case android . media . AudioManager . AUDIOFOCUS_GAIN_TRANSIENT :
429
+ // You have audio focus for a short time
430
+ break ;
431
+ case android . media . AudioManager . AUDIOFOCUS_LOSS :
432
+ this . pause ( ) ;
433
+ break ;
434
+ case android . media . AudioManager . AUDIOFOCUS_LOSS_TRANSIENT :
435
+ // Temporary loss of audio focus - expect to get it back - you can keep your resources around
436
+ this . pause ( ) ;
437
+ break ;
438
+ case android . media . AudioManager . AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK :
439
+ // Lower the volume, keep playing
440
+ this . _lastPlayerVolume = this . volume ;
441
+ this . volume = 0.2 ;
442
+ break ;
443
+ }
420
444
}
421
445
}
422
- } ) ;
446
+ ) ;
423
447
}
0 commit comments