Skip to content

Commit 3f77cb9

Browse files
committed
fixed on started event, added event on paused, on resumed and on stopped
1 parent ec73fc1 commit 3f77cb9

File tree

7 files changed

+154
-31
lines changed

7 files changed

+154
-31
lines changed

build/@types/web-audio-api-player/library/sound.d.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ export interface IOnProgress {
99
export interface IOnEnded {
1010
(willPlayNext: boolean): void;
1111
}
12-
export interface IOnStarted {
13-
(): void;
12+
export interface IOnAnyAction {
13+
(playTimeOffset: number): void;
1414
}
1515
export interface ISoundAttributes {
1616
sources?: (ISoundSource | string)[] | string;
1717
id: number;
1818
playlistId?: number | null;
1919
loop?: boolean;
20-
onLoading?: IOnProgress;
21-
onPlaying?: IOnProgress;
22-
onEnded?: IOnEnded;
23-
onStarted?: IOnStarted;
2420
audioBuffer?: AudioBuffer | null;
2521
arrayBuffer?: ArrayBuffer | null;
2622
duration?: number | null;
23+
onLoading?: IOnProgress;
24+
onPlaying?: IOnProgress;
25+
onEnded?: IOnEnded;
26+
onStarted?: IOnAnyAction;
27+
onStopped?: IOnAnyAction;
28+
onPaused?: IOnAnyAction;
29+
onResumed?: IOnAnyAction;
2730
}
2831
export interface ISound extends ISoundAttributes, IRequested {
2932
sourceNode: AudioBufferSourceNode | null;
@@ -40,6 +43,7 @@ export interface ISound extends ISoundAttributes, IRequested {
4043
sources: (ISoundSource | string)[];
4144
codec: string | null;
4245
duration: number | null;
46+
firstTimePlayed: boolean;
4347
}
4448
export interface IOptions {
4549
}
@@ -63,9 +67,13 @@ export declare class PlayerSound implements ISound {
6367
loadingProgress: number;
6468
codec: string;
6569
duration: number | null;
70+
firstTimePlayed: boolean;
6671
onLoading: IOnProgress;
6772
onPlaying: IOnProgress;
68-
onStarted: IOnStarted;
6973
onEnded: IOnEnded;
74+
onStarted: IOnAnyAction;
75+
onStopped: IOnAnyAction;
76+
onPaused: IOnAnyAction;
77+
onResumed: IOnAnyAction;
7078
constructor(soundAttributes: ISoundAttributes);
7179
}

build/library/core.js

Lines changed: 24 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/library/sound.js

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/simple-player/client/bootstrap.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ $(function () {
3232
console.log('playing: ', playingProgress, maximumValue, currentValue);
3333
playerUI.setPlayingProgress(playingProgress);
3434
},
35-
onStarted: () => {
36-
console.log('started');
35+
onStarted: (playTimeOffset) => {
36+
console.log('started', playTimeOffset);
37+
},
38+
onPaused: (playTimeOffset) => {
39+
console.log('paused', playTimeOffset);
40+
},
41+
onStopped: (playTimeOffset) => {
42+
console.log('stopped', playTimeOffset);
43+
},
44+
onResumed: (playTimeOffset) => {
45+
console.log('resumed', playTimeOffset);
3746
},
3847
onEnded: (willPlayNext) => {
39-
console.log('ended');
48+
console.log('ended', willPlayNext);
4049
if (!willPlayNext) {
4150
playerUI.switchPlayerContext('on');
4251
}
@@ -58,11 +67,20 @@ $(function () {
5867
console.log('playing: ', playingProgress, maximumValue, currentValue);
5968
playerUI.setPlayingProgress(playingProgress);
6069
},
61-
onStarted: () => {
62-
console.log('started');
70+
onStarted: (playTimeOffset) => {
71+
console.log('started', playTimeOffset);
72+
},
73+
onPaused: (playTimeOffset) => {
74+
console.log('paused', playTimeOffset);
75+
},
76+
onStopped: (playTimeOffset) => {
77+
console.log('stopped', playTimeOffset);
78+
},
79+
onResumed: (playTimeOffset) => {
80+
console.log('resumed', playTimeOffset);
6381
},
6482
onEnded: (willPlayNext) => {
65-
console.log('ended');
83+
console.log('ended', willPlayNext);
6684
if (!willPlayNext) {
6785
playerUI.switchPlayerContext('on');
6886
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"scripts": {
2424
"test": "echo \"Error: no test specified\" && exit 1"
2525
},
26-
"version": "0.6.0",
26+
"version": "0.6.1",
2727
"devDependencies": {
2828
"@types/webaudioapi": "0.0.27",
2929
"gulp": "^3.9.1",

source/library/core.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ export class PlayerCore {
407407
loop: sound.loop,
408408
onEnded: () => {
409409
this._onEnded();
410-
},
411-
onPlay: () => { }
410+
}
412411
};
413412

414413
// create a new source node
@@ -430,10 +429,17 @@ export class PlayerCore {
430429
// start(when, offset, duration)
431430
sourceNode.start(0, sound.playTimeOffset);
432431

432+
// trigger resumed event
433+
if (sound.onResumed !== null && !sound.firstTimePlayed) {
434+
sound.onResumed(sound.playTimeOffset);
435+
}
436+
433437
// trigger started event
434-
if (sound.onStarted !== null && sound.playTimeOffset === 0) {
438+
if (sound.onStarted !== null && sound.firstTimePlayed) {
435439

436-
sound.onStarted();
440+
sound.onStarted(sound.playTimeOffset);
441+
442+
sound.firstTimePlayed = false;
437443

438444
}
439445

@@ -487,7 +493,13 @@ export class PlayerCore {
487493

488494
}
489495

490-
this.stop();
496+
// reset the is first time sound is being played to true
497+
currentSound.firstTimePlayed = true;
498+
499+
// reset the playTimeOffset
500+
currentSound.playTimeOffset = 0;
501+
502+
this._stop(currentSound);
491503

492504
if (nextSound !== null) {
493505

@@ -669,6 +681,11 @@ export class PlayerCore {
669681

670682
sound.playTimeOffset += timeAtPause - sound.startTime;
671683

684+
// trigger paused event
685+
if (sound.onPaused !== null) {
686+
sound.onPaused(sound.playTimeOffset);
687+
}
688+
672689
this._stop(sound);
673690

674691
}
@@ -682,6 +699,15 @@ export class PlayerCore {
682699
return;
683700
}
684701

702+
// reset the is first time sound is being played to true
703+
sound.firstTimePlayed = true;
704+
705+
// trigger stopped event
706+
if (sound.onStopped !== null) {
707+
sound.onStopped(sound.playTimeOffset);
708+
}
709+
710+
// reset the playTimeOffset
685711
sound.playTimeOffset = 0;
686712

687713
this._stop(sound);

source/library/sound.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export interface IOnEnded {
1818
(willPlayNext: boolean): void;
1919
}
2020

21-
export interface IOnStarted {
22-
(): void;
21+
export interface IOnAnyAction {
22+
(playTimeOffset: number): void;
2323
}
2424

2525
export interface ISoundAttributes {
@@ -29,13 +29,18 @@ export interface ISoundAttributes {
2929
id: number;
3030
playlistId?: number | null;
3131
loop?: boolean;
32-
onLoading?: IOnProgress;
33-
onPlaying?: IOnProgress;
34-
onEnded?: IOnEnded;
35-
onStarted?: IOnStarted;
3632
audioBuffer?: AudioBuffer | null;
3733
arrayBuffer?: ArrayBuffer | null;
3834
duration?: number | null;
35+
36+
// events
37+
onLoading?: IOnProgress;
38+
onPlaying?: IOnProgress;
39+
onEnded?: IOnEnded;
40+
onStarted?: IOnAnyAction;
41+
onStopped?: IOnAnyAction;
42+
onPaused?: IOnAnyAction;
43+
onResumed?: IOnAnyAction;
3944
}
4045

4146
export interface ISound extends ISoundAttributes, IRequested {
@@ -53,6 +58,7 @@ export interface ISound extends ISoundAttributes, IRequested {
5358
sources: (ISoundSource | string)[];
5459
codec: string | null;
5560
duration: number | null;
61+
firstTimePlayed: boolean;
5662
}
5763

5864
export interface IOptions {
@@ -81,11 +87,16 @@ export class PlayerSound implements ISound {
8187
public loadingProgress: number;
8288
public codec: string;
8389
public duration: number | null;
90+
public firstTimePlayed: boolean;
8491

92+
// events
8593
public onLoading: IOnProgress;
8694
public onPlaying: IOnProgress;
87-
public onStarted: IOnStarted;
8895
public onEnded: IOnEnded;
96+
public onStarted: IOnAnyAction;
97+
public onStopped: IOnAnyAction;
98+
public onPaused: IOnAnyAction;
99+
public onResumed: IOnAnyAction;
89100

90101
constructor(soundAttributes: ISoundAttributes) {
91102

@@ -99,11 +110,14 @@ export class PlayerSound implements ISound {
99110
this.id = soundAttributes.id;
100111
this.playlistId = soundAttributes.playlistId || null;
101112
this.loop = soundAttributes.loop || false;
113+
102114
// the user can set the duration manually
103115
// this is usefull if we need to convert the position percentage into seconds but don't want to preload the song
104116
// to get the duration the song has to get preloaded as the duration is a property of the audioBuffer
105117
this.duration = soundAttributes.duration || null;
106118

119+
this.firstTimePlayed = true;
120+
107121
if (typeof soundAttributes.onLoading === 'function') {
108122
this.onLoading = soundAttributes.onLoading;
109123
} else {
@@ -128,6 +142,24 @@ export class PlayerSound implements ISound {
128142
this.onEnded = null;
129143
}
130144

145+
if (typeof soundAttributes.onStopped === 'function') {
146+
this.onStopped = soundAttributes.onStopped;
147+
} else {
148+
this.onStopped = null;
149+
}
150+
151+
if (typeof soundAttributes.onPaused === 'function') {
152+
this.onPaused = soundAttributes.onPaused;
153+
} else {
154+
this.onPaused = null;
155+
}
156+
157+
if (typeof soundAttributes.onResumed === 'function') {
158+
this.onResumed = soundAttributes.onResumed;
159+
} else {
160+
this.onResumed = null;
161+
}
162+
131163
let arrayBufferType: string = typeof soundAttributes.arrayBuffer;
132164

133165
if (arrayBufferType === 'ArrayBuffer') {

0 commit comments

Comments
 (0)