-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathVideoPlayer.java
More file actions
138 lines (119 loc) · 4.3 KB
/
VideoPlayer.java
File metadata and controls
138 lines (119 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* 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.Plugin;
import org.apache.cordova.api.PluginResult;
public class VideoPlayer extends Plugin {
private static final String YOU_TUBE = "youtube.com";
private static final String ASSETS = "file:///android_asset/";
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("playVideo")) {
playVideo(args.getString(0));
} else {
status = PluginResult.Status.INVALID_ACTION;
}
return new PluginResult(status, result);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} catch (IOException e) {
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
}
}
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
if(isPackageInstalled("com.google.android.youtube")){
//Check if user has YouTube app, if no, will handle it (ChildBrowser/Open Browser/etc.)
uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
intent = new Intent(Intent.ACTION_VIEW, uri);
}else{
throw new IOException("YouTube app isn't installed");
}
} 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 isPackageInstalled(String uri) {
PackageManager pm = this.cordova.getActivity().getPackageManager();
boolean package_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
package_installed = true;
} catch (PackageManager.NameNotFoundException e) {
package_installed = false;
}
return package_installed;
}
}