diff --git a/3.0.0/README.md b/3.0.0/README.md new file mode 100644 index 0000000..a8729cf --- /dev/null +++ b/3.0.0/README.md @@ -0,0 +1,121 @@ +# VideoPlayer plugin for Phonegap/Cordova Android 3.X.X # + +The video player allows you to display videos from your PhoneGap application. +This command fires an Intent to have your devices video player show the video. + +## Adding the Plugin to your project ## + +This plugin is only for the Android platform of Cordova/Phonegap. + +1. To install the plugin, move www/video.js to your project's www folder and include a reference to it in your html file after phonegap.js (or) cordova.js. + + <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
+ <script type="text/javascript" charset="utf-8" src="video.js"></script> + +2. Create a directory within your project called "src/org/apache/cordova/videoplayer" and move VideoPlayer.java into it. + +3. In your res/xml/config.xml file add the following lines: + + <feature name="VideoPlayer">
+ <param name="android-package" value="org.apache.cordova.videoplayer.VideoPlayer" />
+ </feature> + +## Using the plugin ## + +The plugin creates the object `window.plugins.videoPlayer`. To use, call the play() method: + +
+  /**
+	* Display an intent to play the video.
+    *
+    * @param url           The url to play
+    */
+  play(url)
+
+ +Sample use: + + window.plugins.videoPlayer.play("http://path.to.my/video.mp4"); + window.plugins.videoPlayer.play("file:///path/to/my/video.mp4"); + window.plugins.videoPlayer.play("file:///android_asset/www/path/to/my/video.mp4"); + window.plugins.videoPlayer.play("https://www.youtube.com/watch?v=en_sVVjWFKk"); + +Note: When playing video from the assets folder, the video is first copied to internal storage with MODE_WORLD_READABLE. + +## RELEASE NOTES ## + +### October 2, 2011 ### + +* Initial release + + +## BUGS AND CONTRIBUTIONS ## + + +## LICENSE ## + +PhoneGap is available under *either* the terms of the modified BSD license *or* the +MIT License (2008). As a recipient of PhonegGap, you may choose which +license to receive this code under (except as noted in per-module LICENSE +files). Some modules may not be the copyright of Nitobi. These +modules contain explicit declarations of copyright in both the LICENSE files in +the directories in which they reside and in the code itself. No external +contributions are allowed under licenses which are fundamentally incompatible +with the MIT or BSD licenses that PhoneGap is distributed under. + +The text of the MIT and BSD licenses is reproduced below. + +--- + +### The "New" BSD License + +Copyright (c) 2005-2011, Nitobi Software Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of Phonegap/Nitobi nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +--- + +### The MIT License + +Copyright (c) <2011> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + diff --git a/3.0.0/src/org/apache/cordova/videoplayer/VideoPlayer.java b/3.0.0/src/org/apache/cordova/videoplayer/VideoPlayer.java new file mode 100644 index 0000000..1aa938f --- /dev/null +++ b/3.0.0/src/org/apache/cordova/videoplayer/VideoPlayer.java @@ -0,0 +1,132 @@ +/* + * PhoneGap is available under *either* the terms of the modified BSD license *or* the + * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. + * + * Copyright (c) 2005-2010, Nitobi Software Inc. + * Copyright (c) 2011, IBM Corporation + */ + +package org.apache.cordova.videoplayer; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import org.json.JSONArray; +import org.json.JSONException; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.net.Uri; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; + +public class VideoPlayer extends CordovaPlugin { + private static final String YOU_TUBE = "youtube.com"; + private static final String ASSETS = "file:///android_asset/"; + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + PluginResult.Status status = PluginResult.Status.OK; + String result = ""; + + try { + if (action.equals("playVideo")) { + playVideo(args.getString(0)); + } + else { + status = PluginResult.Status.INVALID_ACTION; + } + callbackContext.sendPluginResult(new PluginResult(status, result)); + } catch (JSONException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + } catch (IOException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION)); + } + return true; + } + + private void playVideo(String url) throws IOException { + if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { + //support for google / bitly / tinyurl / youtube shortens + URLConnection con = new URL(url).openConnection(); + con.connect(); + InputStream is = con.getInputStream(); + //new redirected url + url = con.getURL().toString(); + is.close(); + } + + // Create URI + Uri uri = Uri.parse(url); + + Intent intent = null; + // Check to see if someone is trying to play a YouTube page. + if (url.contains(YOU_TUBE)) { + // If we don't do it this way you don't have the option for youtube + uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v")); + if (isYouTubeInstalled()) { + intent = new Intent(Intent.ACTION_VIEW, uri); + } else { + intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse("market://details?id=com.google.android.youtube")); + } + } else if(url.contains(ASSETS)) { + // get file path in assets folder + String filepath = url.replace(ASSETS, ""); + // get actual filename from path as command to write to internal storage doesn't like folders + String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); + + // Don't copy the file if it already exists + File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); + if (!fp.exists()) { + this.copy(filepath, filename); + } + + // change uri to be to the new file in internal storage + uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); + + // Display video player + intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(uri, "video/*"); + } else { + // Display video player + intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(uri, "video/*"); + } + + this.cordova.getActivity().startActivity(intent); + } + + private void copy(String fileFrom, String fileTo) throws IOException { + // get file to be copied from assets + InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); + // get file where copied too, in internal storage. + // must be MODE_WORLD_READABLE or Android can't play it + FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); + + // Transfer bytes from in to out + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) + out.write(buf, 0, len); + in.close(); + out.close(); + } + + private boolean isYouTubeInstalled() { + PackageManager pm = this.cordova.getActivity().getPackageManager(); + try { + pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); + return true; + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } +} \ No newline at end of file diff --git a/3.0.0/www/video.js b/3.0.0/www/video.js new file mode 100644 index 0000000..43ee2f5 --- /dev/null +++ b/3.0.0/www/video.js @@ -0,0 +1,24 @@ +cordova.define("cordova/plugin/videoplayer", + function(require, exports, module) { + var exec = require("cordova/exec"); + var VideoPlayer = function () {}; + + /** + * Starts the video player intent + * + * @param url The url to play + */ + VideoPlayer.prototype.play = function(url) { + exec(null, null, "VideoPlayer", "playVideo", [url]); + }; + + var videoPlayer = new VideoPlayer(); + module.exports = videoPlayer; +}); + +if (!window.plugins) { + window.plugins = {}; +} +if (!window.plugins.videoPlayer) { + window.plugins.videoPlayer = cordova.require("cordova/plugin/videoplayer"); +} \ No newline at end of file diff --git a/README.md b/README.md index e68d970..f08432a 100755 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ The video player allows you to display videos from your PhoneGap application. This command fires an Intent to have your devices video player show the video. +## Cordova/Phonegap 3.0.0 Notes ## + + The plugin architecture for 3.X.X has changed. + Please take a look at the README file inside the 3.X.X directories + ## Adding the Plugin to your project ## Using this plugin requires [Android PhoneGap](https://github.com/apache/incubator-cordova-android).