2009-11-18 02:38:49 +08:00
|
|
|
/**
|
2010-09-01 04:39:37 +08:00
|
|
|
* List of media objects.
|
2009-11-18 02:38:49 +08:00
|
|
|
*/
|
2010-09-01 04:39:37 +08:00
|
|
|
PhoneGap.mediaObjects = {};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
PhoneGap.Media = function() {};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get the media object.
|
|
|
|
* PRIVATE
|
|
|
|
*
|
|
|
|
* @param id The media object id (string)
|
|
|
|
*/
|
|
|
|
PhoneGap.Media.getMediaObject = function(id) {
|
|
|
|
return PhoneGap.mediaObjects[id];
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Audio has status update.
|
|
|
|
* PRIVATE
|
|
|
|
*
|
|
|
|
* @param id The media object id (string)
|
|
|
|
* @param status The status code (int)
|
|
|
|
* @param msg The status message (string)
|
|
|
|
*/
|
|
|
|
PhoneGap.Media.onStatus = function(id, msg, value) {
|
|
|
|
var media = PhoneGap.mediaObjects[id];
|
|
|
|
|
|
|
|
// If state update
|
|
|
|
if (msg == Media.MEDIA_STATE) {
|
|
|
|
if (value == Media.MEDIA_STOPPED) {
|
|
|
|
if (media.successCallback) {
|
|
|
|
media.successCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (media.statusCallback) {
|
|
|
|
media.statusCallback(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg == Media.MEDIA_DURATION) {
|
|
|
|
media._duration = value;
|
|
|
|
}
|
|
|
|
else if (msg == Media.MEDIA_ERROR) {
|
|
|
|
if (media.errorCallback) {
|
|
|
|
media.errorCallback(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* This class provides access to the device media, interfaces to both sound and video
|
|
|
|
*
|
|
|
|
* @param src The file name or url to play
|
|
|
|
* @param successCallback The callback to be called when the file is done playing or recording.
|
|
|
|
* successCallback()
|
|
|
|
* @param errorCallback The callback to be called if there is an error.
|
|
|
|
* errorCallback(int errorCode)
|
|
|
|
* @param statusCallback The callback to be called when media status has changed.
|
|
|
|
* statusCallback(int statusCode)
|
|
|
|
*/
|
|
|
|
Media = function(src, successCallback, errorCallback, statusCallback) {
|
|
|
|
this.id = PhoneGap.createUUID();
|
|
|
|
PhoneGap.mediaObjects[this.id] = this;
|
|
|
|
this.src = src;
|
|
|
|
this.successCallback = successCallback;
|
|
|
|
this.errorCallback = errorCallback;
|
|
|
|
this.statusCallback = statusCallback;
|
|
|
|
this._duration = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Media messages
|
|
|
|
Media.MEDIA_STATE = 1;
|
|
|
|
Media.MEDIA_DURATION = 2;
|
|
|
|
Media.MEDIA_ERROR = 3;
|
|
|
|
|
|
|
|
// Media states
|
|
|
|
Media.MEDIA_NONE = 0;
|
|
|
|
Media.MEDIA_STARTING = 1;
|
|
|
|
Media.MEDIA_RUNNING = 2;
|
|
|
|
Media.MEDIA_PAUSED = 3;
|
|
|
|
Media.MEDIA_STOPPED = 4;
|
|
|
|
Media.MEDIA_MSG = ["None", "Starting", "Running", "Paused", "Stopped"];
|
|
|
|
|
|
|
|
// TODO: Will MediaError be used?
|
2009-11-18 02:38:49 +08:00
|
|
|
/**
|
|
|
|
* This class contains information about any Media errors.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function MediaError() {
|
2010-09-01 04:39:37 +08:00
|
|
|
this.code = null,
|
|
|
|
this.message = "";
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
MediaError.MEDIA_ERR_ABORTED = 1;
|
|
|
|
MediaError.MEDIA_ERR_NETWORK = 2;
|
|
|
|
MediaError.MEDIA_ERR_DECODE = 3;
|
2009-11-18 02:38:49 +08:00
|
|
|
MediaError.MEDIA_ERR_NONE_SUPPORTED = 4;
|
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Start or resume playing audio file.
|
|
|
|
*/
|
|
|
|
Media.prototype.play = function() {
|
|
|
|
GapAudio.startPlayingAudio(this.id, this.src);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Stop playing audio file.
|
|
|
|
*/
|
|
|
|
Media.prototype.stop = function() {
|
|
|
|
GapAudio.stopPlayingAudio(this.id);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
|
|
|
/**
|
2010-09-01 04:39:37 +08:00
|
|
|
* Pause playing audio file.
|
2009-11-18 02:38:49 +08:00
|
|
|
*/
|
2010-09-01 04:39:37 +08:00
|
|
|
Media.prototype.pause = function() {
|
|
|
|
GapAudio.pausePlayingAudio(this.id);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get duration of an audio file.
|
|
|
|
* The duration is only set for audio that is playing, paused or stopped.
|
|
|
|
*
|
|
|
|
* @return duration or -1 if not known.
|
|
|
|
*/
|
|
|
|
Media.prototype.getDuration = function() {
|
|
|
|
return this._duration;
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Get position of audio.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Media.prototype.getCurrentPosition = function() {
|
|
|
|
return GapAudio.getCurrentPositionAudio(this.id);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Start recording audio file.
|
|
|
|
*/
|
2009-11-18 02:38:49 +08:00
|
|
|
Media.prototype.startRecord = function() {
|
2010-09-01 04:39:37 +08:00
|
|
|
GapAudio.startRecordingAudio(this.id, this.src);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|
2010-09-01 04:39:37 +08:00
|
|
|
/**
|
|
|
|
* Stop recording audio file.
|
|
|
|
*/
|
|
|
|
Media.prototype.stopRecord = function() {
|
|
|
|
GapAudio.stopRecordingAudio(this.id);
|
|
|
|
};
|
2009-11-18 02:38:49 +08:00
|
|
|
|