1.js插件开发
描述:提供给web开发端的接口定义,定义了一个VideoPlayer类和play函数,参数为要播放的文件视频地址。
示例代码:
/** * Constructor */ function VideoPlayer() { }; /** * Starts the video player intent * * @param url The url to play */ VideoPlayer.prototype.play = function(url) { PhoneGap.exec(null, null, "VideoPlayer", "playVideo", [url]); }; /** * Load VideoPlayer */ PhoneGap.addConstructor(function() { PhoneGap.addPlugin("videoPlayer", new VideoPlayer()); }); |
2.VideoPlayer类
package com.phonegap.plugins.video; import org.json.JSONArray; import org.json.JSONException; import android.content.Intent; import android.net.Uri; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; public class VideoPlayer extends Plugin { private static final String YOU_TUBE = "youtube.com"; @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); } } private void playVideo(String url) { // 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 intent = new Intent(Intent.ACTION_VIEW, uri); } else { // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } this.ctx.startActivity(intent); } } |
3.配置插件
在res/xml/plugins.xml中添加
<plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/> |
4.调用插件
在html文件中添加
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script> <script type="text/javascript" charset="utf-8" src="video.js"></script> //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"); |