/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ if (!Cordova.hasResource("media")) { Cordova.addResource("media"); /** * This class provides access to the device media, interfaces to both sound and video * * @constructor * @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() - OPTIONAL * @param errorCallback The callback to be called if there is an error. * errorCallback(int errorCode) - OPTIONAL * @param statusCallback The callback to be called when media status has changed. * statusCallback(int statusCode) - OPTIONAL * @param positionCallback The callback to be called when media position has changed. * positionCallback(long position) - OPTIONAL */ var Media = function(src, successCallback, errorCallback, statusCallback, positionCallback) { // successCallback optional if (successCallback && (typeof successCallback !== "function")) { console.log("Media Error: successCallback is not a function"); return; } // errorCallback optional if (errorCallback && (typeof errorCallback !== "function")) { console.log("Media Error: errorCallback is not a function"); return; } // statusCallback optional if (statusCallback && (typeof statusCallback !== "function")) { console.log("Media Error: statusCallback is not a function"); return; } // statusCallback optional if (positionCallback && (typeof positionCallback !== "function")) { console.log("Media Error: positionCallback is not a function"); return; } this.id = Cordova.createUUID(); Cordova.mediaObjects[this.id] = this; this.src = src; this.successCallback = successCallback; this.errorCallback = errorCallback; this.statusCallback = statusCallback; this.positionCallback = positionCallback; this._duration = -1; this._position = -1; }; // Media messages Media.MEDIA_STATE = 1; Media.MEDIA_DURATION = 2; Media.MEDIA_POSITION = 3; Media.MEDIA_ERROR = 9; // 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? /** * This class contains information about any Media errors. * @constructor */ var MediaError = function() { this.code = null; this.message = ""; }; MediaError.MEDIA_ERR_NONE_ACTIVE = 0; MediaError.MEDIA_ERR_ABORTED = 1; MediaError.MEDIA_ERR_NETWORK = 2; MediaError.MEDIA_ERR_DECODE = 3; MediaError.MEDIA_ERR_NONE_SUPPORTED = 4; /** * Start or resume playing audio file. */ Media.prototype.play = function() { Cordova.exec(null, null, "Media", "startPlayingAudio", [this.id, this.src]); }; /** * Stop playing audio file. */ Media.prototype.stop = function() { return Cordova.exec(null, null, "Media", "stopPlayingAudio", [this.id]); }; /** * Seek or jump to a new time in the track.. */ Media.prototype.seekTo = function(milliseconds) { Cordova.exec(null, null, "Media", "seekToAudio", [this.id, milliseconds]); }; /** * Pause playing audio file. */ Media.prototype.pause = function() { Cordova.exec(null, null, "Media", "pausePlayingAudio", [this.id]); }; /** * 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; }; /** * Get position of audio. */ Media.prototype.getCurrentPosition = function(success, fail) { Cordova.exec(success, fail, "Media", "getCurrentPositionAudio", [this.id]); }; /** * Start recording audio file. */ Media.prototype.startRecord = function() { Cordova.exec(null, null, "Media", "startRecordingAudio", [this.id, this.src]); }; /** * Stop recording audio file. */ Media.prototype.stopRecord = function() { Cordova.exec(null, null, "Media", "stopRecordingAudio", [this.id]); }; /** * Release the resources. */ Media.prototype.release = function() { Cordova.exec(null, null, "Media", "release", [this.id]); }; /** * Adjust the volume. */ Media.prototype.setVolume = function(volume) { Cordova.exec(null, null, "Media", "setVolume", [this.id, volume]); }; /** * List of media objects. * PRIVATE */ Cordova.mediaObjects = {}; /** * Object that receives native callbacks. * PRIVATE * @constructor */ Cordova.Media = function() {}; /** * Get the media object. * PRIVATE * * @param id The media object id (string) */ Cordova.Media.getMediaObject = function(id) { return Cordova.mediaObjects[id]; }; /** * Audio has status update. * PRIVATE * * @param id The media object id (string) * @param status The status code (int) * @param msg The status message (string) */ Cordova.Media.onStatus = function(id, msg, value) { var media = Cordova.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({"code":value}); } } else if (msg === Media.MEDIA_POSITION) { media._position = value; } }; }