ionic-audio
Version:
An audio player for Ionic 3 and Angular 5
382 lines • 10.8 kB
JavaScript
import { Injectable, NgZone } from '@angular/core';
/**
* Cordova Media audio track
*
* @export
* @class CordovaAudioTrack
* @constructor
* @implements {IAudioTrack}
*/
var CordovaAudioTrack = (function () {
function CordovaAudioTrack(src) {
this.src = src;
this.isPlaying = false;
this.isFinished = false;
this._progress = 0;
this._completed = 0;
if (window['cordova'] === undefined || window['Media'] === undefined) {
console.log('Cordova Media is not available');
return;
}
;
this._ngZone = new NgZone({ enableLongStackTrace: false });
this.createAudio();
}
CordovaAudioTrack.prototype.createAudio = function () {
var _this = this;
this.audio = new Media(this.src, function () {
console.log('Finished playback');
_this.stopTimer();
_this._ngZone.run(function () {
_this._progress = 0;
_this._completed = 0;
_this._hasLoaded = false;
_this.isFinished = true;
_this.isPlaying = false;
});
_this.destroy(); // TODO add parameter to control whether to release audio on stop or finished
}, function (err) {
console.log("Audio error => track " + _this.src, err);
}, function (status) {
_this._ngZone.run(function () {
switch (status) {
case Media.MEDIA_STARTING:
console.log("Loaded track " + _this.src);
_this._hasLoaded = true;
break;
case Media.MEDIA_RUNNING:
console.log("Playing track " + _this.src);
_this.isPlaying = true;
_this._isLoading = false;
break;
case Media.MEDIA_PAUSED:
_this.isPlaying = false;
break;
case Media.MEDIA_STOPPED:
_this.isPlaying = false;
break;
}
});
});
};
CordovaAudioTrack.prototype.startTimer = function () {
var _this = this;
this._timer = setInterval(function () {
if (_this._duration === undefined) {
var duration = _this.audio.getDuration();
(duration > 0) && (_this._duration = Math.round(_this.audio.getDuration() * 100) / 100);
}
_this.audio.getCurrentPosition(function (position) {
return _this._ngZone.run(function () {
if (position > -1) {
_this._progress = Math.round(position * 100) / 100;
_this._completed = _this._duration > 0 ? Math.round(_this._progress / _this._duration * 100) / 100 : 0;
}
});
}, function (e) {
console.log("Error getting position", e);
});
}, 1000);
};
CordovaAudioTrack.prototype.stopTimer = function () {
clearInterval(this._timer);
};
Object.defineProperty(CordovaAudioTrack.prototype, "id", {
/** public members */
/**
* Gets the track id
*
* @property id
* @type {number}
*/
get: /** public members */
/**
* Gets the track id
*
* @property id
* @type {number}
*/
function () {
return this._id;
},
/**
* Sets the track id
*
* @property id
*/
set: /**
* Sets the track id
*
* @property id
*/
function (v) {
this._id = v;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "duration", {
/**
* Gets the track duration, or -1 if it cannot be determined
*
* @property duration
* @readonly
* @type {number}
*/
get: /**
* Gets the track duration, or -1 if it cannot be determined
*
* @property duration
* @readonly
* @type {number}
*/
function () {
return this._duration;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "progress", {
/**
* Gets current track time (progress)
*
* @property progress
* @readonly
* @type {number}
*/
get: /**
* Gets current track time (progress)
*
* @property progress
* @readonly
* @type {number}
*/
function () {
return this._progress;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "completed", {
/**
* Gets current track progress as a percentage
*
* @property completed
* @readonly
* @type {number}
*/
get: /**
* Gets current track progress as a percentage
*
* @property completed
* @readonly
* @type {number}
*/
function () {
return this._completed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "error", {
/**
* Gets any errors logged by HTML5 audio
*
* @property error
* @readonly
* @type {MediaError}
*/
get: /**
* Gets any errors logged by HTML5 audio
*
* @property error
* @readonly
* @type {MediaError}
*/
function () {
return this.audio.error;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "canPlay", {
/**
* Gets a boolean value indicating whether the current source can be played
*
* @property canPlay
* @readonly
* @type {boolean}
*/
get: /**
* Gets a boolean value indicating whether the current source can be played
*
* @property canPlay
* @readonly
* @type {boolean}
*/
function () {
return true;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "isLoading", {
/**
* Gets a boolean value indicating whether the track is in loading state
*
* @property isLoading
* @readonly
* @type {boolean}
*/
get: /**
* Gets a boolean value indicating whether the track is in loading state
*
* @property isLoading
* @readonly
* @type {boolean}
*/
function () {
return this._isLoading;
},
enumerable: true,
configurable: true
});
Object.defineProperty(CordovaAudioTrack.prototype, "hasLoaded", {
/**
* Gets a boolean value indicating whether the track has finished loading
*
* @property hadLoaded
* @readonly
* @type {boolean}
*/
get: /**
* Gets a boolean value indicating whether the track has finished loading
*
* @property hadLoaded
* @readonly
* @type {boolean}
*/
function () {
return this._hasLoaded;
},
enumerable: true,
configurable: true
});
/**
* Plays current track
*
* @method play
*/
/**
* Plays current track
*
* @method play
*/
CordovaAudioTrack.prototype.play = /**
* Plays current track
*
* @method play
*/
function () {
if (!this.audio) {
this.createAudio();
}
if (!this._hasLoaded) {
console.log("Loading track " + this.src);
this._isLoading = true;
}
this.audio.play();
this.startTimer();
};
/**
* Pauses current track
*
* @method pause
*/
/**
* Pauses current track
*
* @method pause
*/
CordovaAudioTrack.prototype.pause = /**
* Pauses current track
*
* @method pause
*/
function () {
if (!this.isPlaying)
return;
console.log("Pausing track " + this.src);
this.audio.pause();
this.stopTimer();
};
/**
* Stops current track and releases audio
*
* @method stop
*/
/**
* Stops current track and releases audio
*
* @method stop
*/
CordovaAudioTrack.prototype.stop = /**
* Stops current track and releases audio
*
* @method stop
*/
function () {
this.audio.stop(); // calls Media onSuccess callback
};
/**
* Seeks to a new position within the track
*
* @method seekTo
* @param {number} time the new position (milliseconds) to seek to
*/
/**
* Seeks to a new position within the track
*
* @method seekTo
* @param {number} time the new position (milliseconds) to seek to
*/
CordovaAudioTrack.prototype.seekTo = /**
* Seeks to a new position within the track
*
* @method seekTo
* @param {number} time the new position (milliseconds) to seek to
*/
function (time) {
// Cordova Media reports duration and progress as seconds, so we need to multiply by 1000
this.audio.seekTo(time * 1000);
};
/**
* Releases audio resources
*
* @method destroy
*/
/**
* Releases audio resources
*
* @method destroy
*/
CordovaAudioTrack.prototype.destroy = /**
* Releases audio resources
*
* @method destroy
*/
function () {
this.audio.release();
console.log("Released track " + this.src);
};
CordovaAudioTrack.decorators = [
{ type: Injectable },
];
/** @nocollapse */
CordovaAudioTrack.ctorParameters = function () { return [
null,
]; };
return CordovaAudioTrack;
}());
export { CordovaAudioTrack };
//# sourceMappingURL=ionic-audio-cordova-track.js.map