@@ -154,15 +154,15 @@ function getGlobalMixingManager(): AudioFocusManager {
154
154
return globalMixingManager ;
155
155
}
156
156
157
- export class TNSPlayer {
157
+ export class TNSPlayer extends Observable {
158
158
private _mediaPlayer : android . media . MediaPlayer ;
159
159
private _lastPlayerVolume ; // ref to the last volume setting so we can reset after ducking
160
160
private _wasPlaying = false ;
161
- private _events : Observable ;
162
161
private _options : AudioPlayerOptions ;
163
162
private _audioFocusManager : AudioFocusManager | null ;
164
163
165
164
constructor ( durationHint : AudioFocusDurationHint | AudioFocusManager = AudioFocusDurationHint . AUDIOFOCUS_GAIN ) {
165
+ super ( ) ;
166
166
if ( ! ( durationHint instanceof AudioFocusManager ) ) {
167
167
this . setAudioFocusManager (
168
168
new AudioFocusManager ( {
@@ -174,13 +174,6 @@ export class TNSPlayer {
174
174
}
175
175
}
176
176
177
- public get events ( ) {
178
- if ( ! this . _events ) {
179
- this . _events = new Observable ( ) ;
180
- }
181
- return this . _events ;
182
- }
183
-
184
177
get android ( ) : any {
185
178
return this . _player ;
186
179
}
@@ -238,22 +231,22 @@ export class TNSPlayer {
238
231
if ( options . autoPlay !== false ) {
239
232
options . autoPlay = true ;
240
233
}
241
-
234
+ const player = this . _player ;
242
235
const audioPath = resolveAudioFilePath ( options . audioFile ) ;
243
- this . _player . setAudioStreamType ( android . media . AudioManager . STREAM_MUSIC ) ;
244
- this . _player . reset ( ) ;
245
- this . _player . setDataSource ( audioPath ) ;
236
+ player . setAudioStreamType ( android . media . AudioManager . STREAM_MUSIC ) ;
237
+ player . reset ( ) ;
238
+ player . setDataSource ( audioPath ) ;
246
239
247
240
// check if local file or remote - local then `prepare` is okay https://developer.android.com/reference/android/media/MediaPlayer.html#prepare()
248
241
if ( Utils . isFileOrResourcePath ( audioPath ) ) {
249
- this . _player . prepare ( ) ;
242
+ player . prepare ( ) ;
250
243
} else {
251
- this . _player . prepareAsync ( ) ;
244
+ player . prepareAsync ( ) ;
252
245
}
253
246
254
247
// On Info
255
248
if ( options . infoCallback ) {
256
- this . _player . setOnInfoListener (
249
+ player . setOnInfoListener (
257
250
new android . media . MediaPlayer . OnInfoListener ( {
258
251
onInfo : ( player : any , info : number , extra : number ) => {
259
252
options . infoCallback ( { player, info, extra } ) ;
@@ -264,13 +257,17 @@ export class TNSPlayer {
264
257
}
265
258
266
259
// On Prepared
267
- this . _player . setOnPreparedListener (
260
+ player . setOnPreparedListener (
268
261
new android . media . MediaPlayer . OnPreparedListener ( {
269
262
onPrepared : ( mp ) => {
270
- if ( options . autoPlay ) {
271
- this . play ( ) ;
263
+ try {
264
+ if ( options . autoPlay ) {
265
+ this . play ( ) ;
266
+ }
267
+ resolve ( null ) ;
268
+ } catch ( error ) {
269
+ reject ( error ) ;
272
270
}
273
- resolve ( null ) ;
274
271
}
275
272
} )
276
273
) ;
@@ -300,12 +297,13 @@ export class TNSPlayer {
300
297
// We abandon the audio focus but we still preserve
301
298
// the MediaPlayer so we can resume it in the future
302
299
this . _abandonAudioFocus ( true ) ;
303
- this . _sendEvent ( AudioPlayerEvents . paused ) ;
300
+ this . notify ( { eventName : AudioPlayerEvents . paused } ) ;
304
301
}
305
302
}
306
303
307
304
public async play ( ) {
308
- if ( this . _player && ! this . _player . isPlaying ( ) ) {
305
+ const player = this . _player ;
306
+ if ( player && ! player . isPlaying ( ) ) {
309
307
// request audio focus, this will setup the onAudioFocusChangeListener
310
308
if ( this . _options . audioMixing ) {
311
309
// we're mixing audio, so we use a global mixing manager
@@ -318,10 +316,11 @@ export class TNSPlayer {
318
316
throw new Error ( 'Could not request audio focus' ) ;
319
317
}
320
318
321
- this . _sendEvent ( AudioPlayerEvents . started ) ;
319
+ this . notify ( { eventName : AudioPlayerEvents . started } ) ;
320
+ const activity = Application . android . foregroundActivity || Application . android . startActivity ;
322
321
// set volume controls
323
322
// https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)
324
- Application . android . foregroundActivity . setVolumeControlStream ( android . media . AudioManager . STREAM_MUSIC ) ;
323
+ activity . setVolumeControlStream ( android . media . AudioManager . STREAM_MUSIC ) ;
325
324
326
325
// register the receiver so when calls or another app takes main audio focus the player pauses
327
326
Application . android . registerBroadcastReceiver ( android . media . AudioManager . ACTION_AUDIO_BECOMING_NOISY , ( context : android . content . Context , intent : android . content . Intent ) => {
@@ -334,23 +333,23 @@ export class TNSPlayer {
334
333
this . _player . setPlaybackParams ( playBackParams ) ;
335
334
}
336
335
337
- this . _player . start ( ) ;
336
+ player . start ( ) ;
338
337
}
339
338
}
340
339
341
340
public resume ( ) : void {
342
341
if ( this . _player ) {
343
342
// We call play so it can request audio focus
344
343
this . play ( ) ;
345
- this . _sendEvent ( AudioPlayerEvents . started ) ;
344
+ this . notify ( { eventName : AudioPlayerEvents . started } ) ;
346
345
}
347
346
}
348
347
349
348
public async seekTo ( time : number ) {
350
349
if ( this . _player ) {
351
350
time = time * 1000 ;
352
351
this . _player . seekTo ( time ) ;
353
- this . _sendEvent ( AudioPlayerEvents . seek ) ;
352
+ this . notify ( { eventName : AudioPlayerEvents . seek } ) ;
354
353
}
355
354
}
356
355
@@ -396,18 +395,6 @@ export class TNSPlayer {
396
395
return duration . toString ( ) ;
397
396
}
398
397
399
- /**
400
- * Notify events by name and optionally pass data
401
- */
402
- private _sendEvent ( eventName : string , data ?: any ) {
403
- if ( this . events ) {
404
- this . events . notify ( {
405
- eventName,
406
- data
407
- } ) ;
408
- }
409
- }
410
-
411
398
/**
412
399
* Helper method to ensure audio focus.
413
400
*/
0 commit comments