You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2015/03/16 10:15:28 UTC

cordova-plugin-media git commit: CB-7962 Adds browser platform support

Repository: cordova-plugin-media
Updated Branches:
  refs/heads/master 478ac90d5 -> a8b6f9e41


CB-7962 Adds browser platform support


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/commit/a8b6f9e4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/tree/a8b6f9e4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/diff/a8b6f9e4

Branch: refs/heads/master
Commit: a8b6f9e41f1359f97e5a67b4c8d5cb7c71a7fab6
Parents: 478ac90
Author: Vladimir Kotikov <v-...@microsoft.com>
Authored: Tue Jan 27 13:15:42 2015 +0300
Committer: Vladimir Kotikov <v-...@microsoft.com>
Committed: Mon Mar 16 12:00:33 2015 +0300

----------------------------------------------------------------------
 README.md            |   2 +
 plugin.xml           |   8 ++
 www/browser/Media.js | 259 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 269 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/a8b6f9e4/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 72c79f8..a0f4768 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,9 @@ Although in the global scope, it is not available until after the `deviceready`
 - iOS
 - Windows Phone 7 and 8
 - Tizen
+- Windows 8
 - Windows
+- Browser
 
 ## Windows Phone Quirks
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/a8b6f9e4/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 4cbb392..88e3fc5 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -177,4 +177,12 @@ id="org.apache.cordova.media"
             <runs/>
         </js-module>
     </platform>
+
+    <!-- browser -->
+    <platform name="browser">
+        <js-module src="www/browser/Media.js" name="BrowserMedia">
+            <clobbers target="window.Media" />
+        </js-module>
+    </platform>
+
 </plugin>

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/a8b6f9e4/www/browser/Media.js
----------------------------------------------------------------------
diff --git a/www/browser/Media.js b/www/browser/Media.js
new file mode 100644
index 0000000..855faac
--- /dev/null
+++ b/www/browser/Media.js
@@ -0,0 +1,259 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+/*global MediaError, module, require*/
+
+var argscheck = require('cordova/argscheck'),
+    utils = require('cordova/utils');
+
+var mediaObjects = {};
+
+/**
+ * Creates new Audio node and with necessary event listeners attached
+ * @param  {Media} media Media object
+ * @return {Audio}       Audio element 
+ */
+function createNode (media) {
+    var node = new Audio();
+
+    node.onloadstart = function () {
+        Media.onStatus(media.id, Media.MEDIA_STATE, Media.MEDIA_STARTING);
+    };
+
+    node.onplaying = function () {
+        Media.onStatus(media.id, Media.MEDIA_STATE, Media.MEDIA_RUNNING);
+    };
+
+    node.ondurationchange = function (e) {
+        Media.onStatus(media.id, Media.MEDIA_DURATION, e.target.duration || -1);
+    };
+
+    node.onerror = function (e) {
+        // Due to media.spec.15 It should return MediaError for bad filename
+        var err = e.target.error.code === MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED ?
+            { code: MediaError.MEDIA_ERR_ABORTED } :
+            e.target.error;
+
+        Media.onStatus(media.id, Media.MEDIA_ERROR, err);
+    };
+
+    node.onended = function () {
+        Media.onStatus(media.id, Media.MEDIA_STATE, Media.MEDIA_STOPPED);
+    };
+
+    if (media.src) {
+        node.src = media.src;
+    }
+
+    return node;
+}
+
+/**
+ * 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()
+ * @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
+ */
+var Media = function(src, successCallback, errorCallback, statusCallback) {
+    argscheck.checkArgs('SFFF', 'Media', arguments);
+    this.id = utils.createUUID();
+    mediaObjects[this.id] = this;
+    this.src = src;
+    this.successCallback = successCallback;
+    this.errorCallback = errorCallback;
+    this.statusCallback = statusCallback;
+    this._duration = -1;
+    this._position = -1;
+
+    Media.onStatus(this.id, Media.MEDIA_STATE, Media.MEDIA_STARTING);
+    
+    try {
+        this.node = createNode(this);
+    } catch (err) {
+        Media.onStatus(this.id, Media.MEDIA_ERROR, { code: MediaError.MEDIA_ERR_ABORTED });
+    }
+};
+
+// 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"];
+
+/**
+ * Start or resume playing audio file.
+ */
+Media.prototype.play = function() {
+
+    // if Media was released, then node will be null and we need to create it again
+    if (!this.node) {
+        try {
+            this.node = createNode(this);
+        } catch (err) {
+            Media.onStatus(this.id, Media.MEDIA_ERROR, { code: MediaError.MEDIA_ERR_ABORTED });
+        }
+    }
+
+    this.node.play();
+};
+
+/**
+ * Stop playing audio file.
+ */
+Media.prototype.stop = function() {
+    try {
+        this.pause();
+        this.seekTo(0);
+        Media.onStatus(this.id, Media.MEDIA_STATE, Media.MEDIA_STOPPED);
+    } catch (err) {
+        Media.onStatus(this.id, Media.MEDIA_ERROR, err);
+    }
+};
+
+/**
+ * Seek or jump to a new time in the track..
+ */
+Media.prototype.seekTo = function(milliseconds) {
+    try {
+        this.node.currentTime = milliseconds / 1000;
+    } catch (err) {
+        Media.onStatus(this.id, Media.MEDIA_ERROR, err);
+    }
+};
+
+/**
+ * Pause playing audio file.
+ */
+Media.prototype.pause = function() {
+    try {
+        this.node.pause();
+        Media.onStatus(this.id, Media.MEDIA_STATE, Media.MEDIA_PAUSED);
+    } catch (err) {
+        Media.onStatus(this.id, Media.MEDIA_ERROR, err);
+    }};
+
+/**
+ * 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) {
+    try {
+        var p = this.node.currentTime;
+        Media.onStatus(this.id, Media.MEDIA_POSITION, p);
+        success(p);
+    } catch (err) {
+        fail(err);
+    }
+};
+
+/**
+ * Start recording audio file.
+ */
+Media.prototype.startRecord = function() {
+    Media.onStatus(this.id, Media.MEDIA_ERROR, "Not supported");
+};
+
+/**
+ * Stop recording audio file.
+ */
+Media.prototype.stopRecord = function() {
+    Media.onStatus(this.id, Media.MEDIA_ERROR, "Not supported");
+};
+
+/**
+ * Release the resources.
+ */
+Media.prototype.release = function() {
+    try {
+        delete this.node;
+    } catch (err) {
+        Media.onStatus(this.id, Media.MEDIA_ERROR, err);
+    }};
+
+/**
+ * Adjust the volume.
+ */
+Media.prototype.setVolume = function(volume) {
+    this.node.volume = volume;
+};
+
+/**
+ * Audio has status update.
+ * PRIVATE
+ *
+ * @param id            The media object id (string)
+ * @param msgType       The 'type' of update this is
+ * @param value         Use of value is determined by the msgType
+ */
+Media.onStatus = function(id, msgType, value) {
+
+    var media = mediaObjects[id];
+
+    if(media) {
+        switch(msgType) {
+            case Media.MEDIA_STATE :
+                media.statusCallback && media.statusCallback(value);
+                if(value === Media.MEDIA_STOPPED) {
+                    media.successCallback && media.successCallback();
+                }
+                break;
+            case Media.MEDIA_DURATION :
+                media._duration = value;
+                break;
+            case Media.MEDIA_ERROR :
+                media.errorCallback && media.errorCallback(value);
+                break;
+            case Media.MEDIA_POSITION :
+                media._position = Number(value);
+                break;
+            default :
+                console.error && console.error("Unhandled Media.onStatus :: " + msgType);
+                break;
+        }
+    } else {
+         console.error && console.error("Received Media.onStatus callback for unknown media :: " + id);
+    }
+};
+
+module.exports = Media;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org