博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 播放视频
阅读量:6934 次
发布时间:2019-06-27

本文共 5597 字,大约阅读时间需要 18 分钟。

 

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

 

转载地址:http://lggjl.baihongyu.com/

你可能感兴趣的文章
CrazyWing:Python自动化运维开发实战 六、流程控制
查看>>
tomcat上传war包失败
查看>>
网页禁止行为的总结
查看>>
使用ISAPI_Rewrite做实用的重定向
查看>>
3.6 迁移故障恢复
查看>>
DTS增量/同步支持DDL迁移的说明
查看>>
java mp3播放器 无界面
查看>>
VII python面向对象
查看>>
程序员在囧途之做私活小记
查看>>
【Go语言】【12】GO语言的结构体
查看>>
python中try Except抛出异常使用方法
查看>>
mysql数据库创建函数过程
查看>>
Mysql show Status参数详解
查看>>
DPI 设置过大该如何还原?
查看>>
运维学习资料(2)
查看>>
<Power Shell>02 认识powershell
查看>>
Oracle日常巡检
查看>>
jQuery用于请求服务器的函数
查看>>
让iis7.5显示php错误的详细信息~
查看>>
C#中字符串的处理
查看>>