Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

Augment iAudioTrack with useful state change events #117

Open
wants to merge 5 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change to use an observable Subject for the IAudioTrack
  • Loading branch information
codinronan committed May 5, 2017
commit d70d37eae9bbf8b9cb24f150ae68a91de07fad27
31 changes: 16 additions & 15 deletions dist/ionic-audio-cordova-track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IAudioTrack, IAudioTrackError} from './ionic-audio-interfaces';
import {Injectable, EventEmitter} from '@angular/core';
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';

declare let Media: any;

Expand Down Expand Up @@ -31,47 +32,47 @@ export class CordovaAudioTrack implements IAudioTrack {
* @property onPlayBegin
* @type {EventEmitter}
*/
onLoaded: EventEmitter<IAudioTrack> = new EventEmitter();
onLoaded: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has begun
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onPlaying: EventEmitter<IAudioTrack> = new EventEmitter();
onPlaying: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has begun
* Notifies when playback has stopped
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onStop: EventEmitter<IAudioTrack> = new EventEmitter();
onStop: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has completed
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onFinished: EventEmitter<IAudioTrack> = new EventEmitter();
onFinished: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has begun
* Notifies when playback progress has changed
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onProgressChange: EventEmitter<IAudioTrack> = new EventEmitter();
onProgressChange: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when the media has experienced an error
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onError: EventEmitter<IAudioTrackError> = new EventEmitter();
onError: Subject<IAudioTrackError> = new Subject<IAudioTrackError>();

constructor(public src: string) {
if (window['cordova'] === undefined || window['Media'] === undefined) {
Expand All @@ -87,31 +88,31 @@ export class CordovaAudioTrack implements IAudioTrack {
console.log('Finished playback');
this.stopTimer();
this.isFinished = true;
this.onFinished.emit(this);
this.onFinished.next(this);
this.destroy(); // TODO add parameter to control whether to release audio on stop or finished
}, (err) => {
console.log(`Audio error => track ${this.src}`, err);
this.onError.emit({track: this, error: err});
this.onError.next({track: this, error: err});
}, (status) => {
switch (status) {
case Media.MEDIA_STARTING:
console.log(`Loaded track ${this.src}`);
this._hasLoaded = true;
this.isLoaded = true;
this.onLoaded.emit(this);
this.onLoaded.next(this);
break;
case Media.MEDIA_RUNNING:
console.log(`Playing track ${this.src}`);
this.isPlaying = true;
this._isLoading = false;
this.onPlaying.emit(this);
this.onPlaying.next(this);
break;
case Media.MEDIA_PAUSED:
this.isPlaying = false;
break
case Media.MEDIA_STOPPED:
this.isPlaying = false;
this.onStop.emit(this);
this.onStop.next(this);
break;
}
});
Expand All @@ -127,7 +128,7 @@ export class CordovaAudioTrack implements IAudioTrack {
if (position > -1) {
this._progress = Math.round(position*100)/100;
this._completed = this._duration > 0 ? Math.round(this._progress / this._duration * 100)/100 : 0;
this.onProgressChange.emit(this);
this.onProgressChange.next(this);
}
}, (e) => {
console.log("Error getting position", e);
Expand Down
14 changes: 7 additions & 7 deletions dist/ionic-audio-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from '@angular/core';
import { Subject } from 'rxjs/Subject';

/**
* Defines the audio provider contract
Expand Down Expand Up @@ -58,12 +58,12 @@ export interface IAudioTrack extends ITrackConstraint {
seekTo(time: number);
destroy();

onLoaded: EventEmitter<IAudioTrack>;
onPlaying: EventEmitter<IAudioTrack>;
onStop: EventEmitter<IAudioTrack>;
onFinished: EventEmitter<IAudioTrack>;
onProgressChange: EventEmitter<IAudioTrack>;
onError: EventEmitter<IAudioTrackError>;
onLoaded: Subject<IAudioTrack>;
onPlaying: Subject<IAudioTrack>;
onStop: Subject<IAudioTrack>;
onFinished: Subject<IAudioTrack>;
onProgressChange: Subject<IAudioTrack>;
onError: Subject<IAudioTrackError>;
}

/**
Expand Down
25 changes: 13 additions & 12 deletions dist/ionic-audio-web-track.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IAudioTrack, IAudioTrackError} from './ionic-audio-interfaces';
import {Injectable, Optional, EventEmitter} from '@angular/core';
import {Injectable, Optional} from '@angular/core';
import {Subject} from 'rxjs/Subject';

declare let window;
window.AudioContext = window['AudioContext'] || window['webkitAudioContext'];
Expand Down Expand Up @@ -32,47 +33,47 @@ export class WebAudioTrack implements IAudioTrack {
* @property onPlayBegin
* @type {EventEmitter}
*/
onLoaded: EventEmitter<IAudioTrack> = new EventEmitter();
onLoaded: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has begun
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onPlaying: EventEmitter<IAudioTrack> = new EventEmitter();
onPlaying: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has stopped
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onStop: EventEmitter<IAudioTrack> = new EventEmitter();
onStop: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback has completed
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onFinished: EventEmitter<IAudioTrack> = new EventEmitter();
onFinished: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when playback progress has changed
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onProgressChange: EventEmitter<IAudioTrack> = new EventEmitter();
onProgressChange: Subject<IAudioTrack> = new Subject<IAudioTrack>();

/**
* Notifies when the media has experienced an error
*
* @property onPlayBegin
* @type {EventEmitter}
*/
onError: EventEmitter<IAudioTrackError> = new EventEmitter();
onError: Subject<IAudioTrackError> = new Subject<IAudioTrackError>();

constructor(public src: string, @Optional() public preload: string = 'none') {
// audio context not needed for now
Expand All @@ -94,28 +95,28 @@ export class WebAudioTrack implements IAudioTrack {
this.audio.addEventListener("error", (err) => {
console.log(`Audio error => track ${this.src}`, err);
this.isPlaying = false;
this.onError.emit({track: this, error: err});
this.onError.next({track: this, error: err});
}, false);

this.audio.addEventListener("canplay", () => {
console.log(`Loaded track ${this.src}`);
this._isLoading = false;
this.isLoaded = true;
this._hasLoaded = true;
this.onLoaded.emit(this);
this.onLoaded.next(this);
}, false);

this.audio.addEventListener("playing", () => {
console.log(`Playing track ${this.src}`);
this.isFinished = false;
this.isPlaying = true;
this.onPlaying.emit(this);
this.onPlaying.next(this);
}, false);

this.audio.addEventListener("ended", () => {
this.isPlaying = false;
this.isFinished = true;
this.onFinished.emit(this);
this.onFinished.next(this);
console.log('Finished playback');
}, false);

Expand All @@ -128,7 +129,7 @@ export class WebAudioTrack implements IAudioTrack {
if (this.isPlaying && this.audio.currentTime > 0) {
this._progress = this.audio.currentTime;
this._completed = this.audio.duration > 0 ? Math.trunc (this.audio.currentTime / this.audio.duration * 100)/100 : 0;
this.onProgressChange.emit(this);
this.onProgressChange.next(this);
}
}

Expand Down