Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phone gap branch 042017 #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added HelloWorld-debug.apk
Binary file not shown.
132 changes: 132 additions & 0 deletions src/com/phonegap/plugins/video/VideoPlayer.java
Original file line number Diff line number Diff line change
@@ -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 com.phonegap.plugins.video;

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.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.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;
}
}
}
2 changes: 1 addition & 1 deletion www/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<!--
<gap:plugin name="com.phonegap.plugins.barcodescanner" />
-->

<plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/>
<!-- Define app icon for each platform. -->
<icon src="icon.png" />
<icon src="res/icon/android/icon-36-ldpi.png" gap:platform="android" gap:density="ldpi" />
Expand Down
11 changes: 8 additions & 3 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
<title>JCM.MARKETING PROJECT ACCESS FOR CLIENT </title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<h1>PROJECT ACCESS</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<h2 class="event received">Hello World!!!</h2>
<div class="event received">

<script> window.plugins.videoPlayer.play("https://player.vimeo.com/external/197451146.sd.mp4?s=5d6024f6ed2b8442b649ffd259e23ebc02b73dd6&profile_id=164");
</script>
</div>
</div>
</div>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8" src="js/video.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
Expand Down
24 changes: 24 additions & 0 deletions www/js/video.js
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions www/res/xml/plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/>