Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset isFinished flags to false to ensure onFinish emit fires more than once #138

Open
wants to merge 1 commit into
base: 3.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 45 additions & 43 deletions src/ionic-audio-track-component.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {ITrackConstraint, IAudioTrack} from './ionic-audio-interfaces';
import {AudioProvider} from './ionic-audio-providers';
import {WebAudioTrack} from './ionic-audio-web-track';
import {CordovaAudioTrack} from './ionic-audio-cordova-track';
import {ITrackConstraint, IAudioTrack} from './ionic-audio-interfaces';
import {AudioProvider} from './ionic-audio-providers';
import {WebAudioTrack} from './ionic-audio-web-track';
import {CordovaAudioTrack} from './ionic-audio-cordova-track';

import {Component, DoCheck, EventEmitter, Output, Input} from '@angular/core';


/**
* # ```<audio-track>```
*
* # ```<audio-track>```
*
* Creates a top level audio-track component
*
*
* ## Usage
*
*
* ````
* <audio-track #audio [track]="myTrack" (onFinish)="onTrackFinished($event)">
* ...
Expand All @@ -26,7 +26,7 @@ import {Component, DoCheck, EventEmitter, Output, Input} from '@angular/core';
selector: 'audio-track',
template: '<ng-content></ng-content>'
})
export class AudioTrackComponent implements DoCheck {
export class AudioTrackComponent implements DoCheck {
/**
* Input property containing a JSON object with at least a src property
* ````
Expand All @@ -42,114 +42,116 @@ export class AudioTrackComponent implements DoCheck {
* @type {ITrackConstraint}
*/
@Input() track: ITrackConstraint;

/**
* Output property expects an event handler to be notified whenever playback finishes
*
*
* @property onFinish
* @type {EventEmitter}
*/
@Output() onFinish = new EventEmitter<ITrackConstraint>();

private _isFinished: boolean = false;
private _audioTrack: IAudioTrack;

constructor(private _audioProvider: AudioProvider) {}

ngOnInit() {
if (!(this.track instanceof WebAudioTrack) && !(this.track instanceof CordovaAudioTrack)) {
this._audioTrack = this._audioProvider.create(this.track);
this._audioTrack = this._audioProvider.create(this.track);
} else {
Object.assign(this._audioTrack, this.track);
this._audioProvider.add(this._audioTrack);
}

// update input track parameter with track is so we pass it to WebAudioProvider if needed
this.track.id = this._audioTrack.id;
this.track.id = this._audioTrack.id;
}
play() {

play() {
this._audioTrack.play();
this._audioProvider.current = this._audioTrack.id;
}

pause() {
this._audioTrack.pause();
this._audioProvider.current = undefined;
}

toggle() {
if (this._audioTrack.isPlaying) {
this.pause();
} else {
this.play();
}
}
}

seekTo(time:number) {
this._audioTrack.seekTo(time);
this._audioTrack.seekTo(time);
}


public get id() : number {
return this._audioTrack.id;
}

public get art() : string {
return this.track.art;
}


public get artist() : string {
return this.track.artist;
}


public get title() : string {
return this.track.title;
}

public get progress() : number {
return this._audioTrack.progress;
}

public get isPlaying() : boolean {
return this._audioTrack.isPlaying;
}

public get duration() : number {
return this._audioTrack.duration;
}

public get completed() : number {
return this._audioTrack.completed;
}

public get canPlay() {
return this._audioTrack.canPlay;
}

public get error() {
return this._audioTrack.error;
}

public get isLoading() : boolean {
return this._audioTrack.isLoading;
}

public get hasLoaded() : boolean {
return this.hasLoaded;
}

ngDoCheck() {
if(!Object.is(this._audioTrack.isFinished, this._isFinished)) {
// some logic here to react to the change
this._isFinished = this._audioTrack.isFinished;

// track has stopped, trigger finish event
if (this._isFinished) {
this.onFinish.emit(this.track);
this.onFinish.emit(this.track);
this._isFinished = false;
this._audioTrack.isFinished = false;
}
}
}
}
}