Skip to content

Commit 5103f5f

Browse files
committed
fix: event fixes
1 parent 5ac784b commit 5103f5f

File tree

3 files changed

+33
-45
lines changed

3 files changed

+33
-45
lines changed

Diff for: src/audio/android/player.ts

+26-39
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ function getGlobalMixingManager(): AudioFocusManager {
154154
return globalMixingManager;
155155
}
156156

157-
export class TNSPlayer {
157+
export class TNSPlayer extends Observable {
158158
private _mediaPlayer: android.media.MediaPlayer;
159159
private _lastPlayerVolume; // ref to the last volume setting so we can reset after ducking
160160
private _wasPlaying = false;
161-
private _events: Observable;
162161
private _options: AudioPlayerOptions;
163162
private _audioFocusManager: AudioFocusManager | null;
164163

165164
constructor(durationHint: AudioFocusDurationHint | AudioFocusManager = AudioFocusDurationHint.AUDIOFOCUS_GAIN) {
165+
super();
166166
if (!(durationHint instanceof AudioFocusManager)) {
167167
this.setAudioFocusManager(
168168
new AudioFocusManager({
@@ -174,13 +174,6 @@ export class TNSPlayer {
174174
}
175175
}
176176

177-
public get events() {
178-
if (!this._events) {
179-
this._events = new Observable();
180-
}
181-
return this._events;
182-
}
183-
184177
get android(): any {
185178
return this._player;
186179
}
@@ -238,22 +231,22 @@ export class TNSPlayer {
238231
if (options.autoPlay !== false) {
239232
options.autoPlay = true;
240233
}
241-
234+
const player = this._player;
242235
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);
246239

247240
// check if local file or remote - local then `prepare` is okay https://developer.android.com/reference/android/media/MediaPlayer.html#prepare()
248241
if (Utils.isFileOrResourcePath(audioPath)) {
249-
this._player.prepare();
242+
player.prepare();
250243
} else {
251-
this._player.prepareAsync();
244+
player.prepareAsync();
252245
}
253246

254247
// On Info
255248
if (options.infoCallback) {
256-
this._player.setOnInfoListener(
249+
player.setOnInfoListener(
257250
new android.media.MediaPlayer.OnInfoListener({
258251
onInfo: (player: any, info: number, extra: number) => {
259252
options.infoCallback({ player, info, extra });
@@ -264,13 +257,17 @@ export class TNSPlayer {
264257
}
265258

266259
// On Prepared
267-
this._player.setOnPreparedListener(
260+
player.setOnPreparedListener(
268261
new android.media.MediaPlayer.OnPreparedListener({
269262
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);
272270
}
273-
resolve(null);
274271
}
275272
})
276273
);
@@ -300,12 +297,13 @@ export class TNSPlayer {
300297
// We abandon the audio focus but we still preserve
301298
// the MediaPlayer so we can resume it in the future
302299
this._abandonAudioFocus(true);
303-
this._sendEvent(AudioPlayerEvents.paused);
300+
this.notify({ eventName: AudioPlayerEvents.paused });
304301
}
305302
}
306303

307304
public async play() {
308-
if (this._player && !this._player.isPlaying()) {
305+
const player = this._player;
306+
if (player && !player.isPlaying()) {
309307
// request audio focus, this will setup the onAudioFocusChangeListener
310308
if (this._options.audioMixing) {
311309
// we're mixing audio, so we use a global mixing manager
@@ -318,10 +316,11 @@ export class TNSPlayer {
318316
throw new Error('Could not request audio focus');
319317
}
320318

321-
this._sendEvent(AudioPlayerEvents.started);
319+
this.notify({ eventName: AudioPlayerEvents.started });
320+
const activity = Application.android.foregroundActivity || Application.android.startActivity;
322321
// set volume controls
323322
// 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);
325324

326325
// register the receiver so when calls or another app takes main audio focus the player pauses
327326
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 {
334333
this._player.setPlaybackParams(playBackParams);
335334
}
336335

337-
this._player.start();
336+
player.start();
338337
}
339338
}
340339

341340
public resume(): void {
342341
if (this._player) {
343342
// We call play so it can request audio focus
344343
this.play();
345-
this._sendEvent(AudioPlayerEvents.started);
344+
this.notify({ eventName: AudioPlayerEvents.started });
346345
}
347346
}
348347

349348
public async seekTo(time: number) {
350349
if (this._player) {
351350
time = time * 1000;
352351
this._player.seekTo(time);
353-
this._sendEvent(AudioPlayerEvents.seek);
352+
this.notify({ eventName: AudioPlayerEvents.seek });
354353
}
355354
}
356355

@@ -396,18 +395,6 @@ export class TNSPlayer {
396395
return duration.toString();
397396
}
398397

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-
411398
/**
412399
* Helper method to ensure audio focus.
413400
*/

Diff for: src/audio/common.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Utils, knownFolders, path as nsFilePath } from '@nativescript/core';
22

3+
export enum IAudioPlayerEvents {
4+
seek = 'seek',
5+
paused = 'paused',
6+
started = 'started'
7+
}
38
/**
49
* Helper function to determine if string is a url.
510
* @param value [string]

Diff for: src/audio/index.d.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Observable } from '@nativescript/core';
2+
3+
export { IAudioPlayerEvents } from './common';
24
export interface AudioPlayerOptions {
35
/**
46
* The audio file to play.
@@ -270,12 +272,6 @@ export declare class TNSRecorder {
270272
audioRecorderDidFinishRecording(recorder: any, success: boolean): void;
271273
}
272274

273-
export interface IAudioPlayerEvents {
274-
seek: 'seek';
275-
paused: 'paused';
276-
started: 'started';
277-
}
278-
279275
export const AudioPlayerEvents: IAudioPlayerEvents;
280276

281277
export enum AudioFocusDurationHint {

0 commit comments

Comments
 (0)