android 播放视频详解手机开发

<script type="text/javascript" charset="utf-8" > 
 //Sample use:  
 /**  
    * Display an intent to play the video.  
    *  
    * @param url           The url to play  
    */  
 //play(url)  
   
    //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"); 
    //console.log(window.plugins); 
    console.log("----1"); 
    window.plugins.videoPlayer.play("file:///android_asset/www/video/iceage4.mp4"); 
    //console.log("----1"); 
    //VideoPlayer.prototype.play("file:///android_asset/www/video/iceage4.mp4"); 
</script>

 

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; 
} 
} 
}

video.js

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"); 
}

config.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
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. 
--> 
<cordova> 
<!-- 
access elements control the Android whitelist. 
Domains are assumed blocked unless set otherwise 
--> 
<access origin="http://127.0.0.1*"/> <!-- allow local pages --> 
<!-- <access origin="https://example.com" /> allow any secure requests to example.com --> 
<!-- <access origin="https://example.com" subdomains="true" /> such as above, but including subdomains, such as www --> 
<access origin=".*"/> 
<log level="DEBUG"/> 
<preference name="useBrowserHistory" value="true" /> 
<preference name="exit-on-suspend" value="false" /> 
<plugins> 
<plugin name="App" value="org.apache.cordova.App"/> 
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/> 
<plugin name="Device" value="org.apache.cordova.Device"/> 
<plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/> 
<plugin name="Compass" value="org.apache.cordova.CompassListener"/> 
<plugin name="Media" value="org.apache.cordova.AudioHandler"/> 
<plugin name="Camera" value="org.apache.cordova.CameraLauncher"/> 
<plugin name="Contacts" value="org.apache.cordova.ContactManager"/> 
<plugin name="File" value="org.apache.cordova.FileUtils"/> 
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/> 
<plugin name="Notification" value="org.apache.cordova.Notification"/> 
<plugin name="Storage" value="org.apache.cordova.Storage"/> 
<plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/> 
<plugin name="Capture" value="org.apache.cordova.Capture"/> 
<plugin name="Battery" value="org.apache.cordova.BatteryListener"/> 
<plugin name="SplashScreen" value="org.apache.cordova.SplashScreen"/> 
<plugin name="Echo" value="org.apache.cordova.Echo" /> 
<plugin name="Globalization" value="org.apache.cordova.Globalization"/> 
<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser"/> 
<plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/> 
</plugins> 
</cordova>

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5992.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论