<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>润物无声 &#187; 插件</title>
	<atom:link href="http://blog.zhourunsheng.com/tag/%e6%8f%92%e4%bb%b6/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.zhourunsheng.com</link>
	<description>天空一朵雨做的云</description>
	<lastBuildDate>Sat, 08 May 2021 05:17:21 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>关于PhoneGap视频插件的代码更新</title>
		<link>http://blog.zhourunsheng.com/2012/04/%e5%85%b3%e4%ba%8ephonegap%e8%a7%86%e9%a2%91%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%96%b0/</link>
		<comments>http://blog.zhourunsheng.com/2012/04/%e5%85%b3%e4%ba%8ephonegap%e8%a7%86%e9%a2%91%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%96%b0/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 06:55:35 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[移动开发]]></category>
		<category><![CDATA[Cordova]]></category>
		<category><![CDATA[PhoneGap]]></category>
		<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=1441</guid>
		<description><![CDATA[<p>本文主要介绍PhoneGap视频插件的代码更新，具体插件的开发过程请参照我以前的博文《phonegap之and [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2012/04/%e5%85%b3%e4%ba%8ephonegap%e8%a7%86%e9%a2%91%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%96%b0/">关于PhoneGap视频插件的代码更新</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>本文主要介绍PhoneGap视频插件的代码更新，具体插件的开发过程请参照我以前的博文《<a href="http://blog.zhourunsheng.com/2011/11/phonegap-%E4%B9%8B-android-%E7%AB%AF%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91/">phonegap之android端插件开发</a>》。现在PhoneGap已经把代码移交到apache开源项目cordova里面，本文代码的更新主要涉及到两项，一是把以前phoneGap的引用迁移到cordova的引用中，二是在原来插件代码的基础上增加回调机制。<span id="more-1441"></span></p>
<p>以前版本的视频插件可以在android设备上面正常工作，但是有一个潜在的问题，因为插件启动视频播放利用的是 “startActivity”方法，无法获取到视频播放结束的消息，本次代码的主要改进就是改变原来启动视频播放的函数，使用“startActivityForResult”方法，这样当视频播放结束的时候，“onActivityResult”方法会被回调，从而该插件就能正确通知插件使用者视频已正常播放结束，可以继续处理其他流程。</p>
<p>Java 端的完整代码如下：</p>
<pre>package com.dlgreat.bbhtml.plugins;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.net.Uri;

public class VideoPlayer extends Plugin {

private String callbackId;

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
   this.callbackId = callbackId;
   PluginResult.Status status = PluginResult.Status.OK;
   String result = "";

   try {
       if (action.equals("playVideo")) {
           playVideo(args.getString(0));

           PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
           res.setKeepCallback(true);
           return res;
       }

       return new PluginResult(status, result);
   } catch (JSONException e) {
       return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
   }
}

private void playVideo(String url) {
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "video/*");

    this.ctx.startActivityForResult(this, intent, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  if (requestCode == 0) {
     success(new PluginResult(PluginResult.Status.OK, ""), callbackId);
  }
}
}</pre>
<p>Javascript端的完整代码如下：</p>
<pre>/* PhoneGap 视频播放插件*/

/**
* Constructor
*/
function VideoPlayer() {};

/**
* Starts the video player intent
*
* @param url The url to play
*/
VideoPlayer.prototype.play = function(url, successCallback, errorCallback) {
   cordova.exec(successCallback, errorCallback, "VideoPlayer", "playVideo", [url]);
};

/**
* Load VideoPlayer
*/
cordova.addConstructor(function() {
    cordova.addPlugin("videoplayer", new VideoPlayer());
});</pre>
<p>插件的配置代码如下（xml/plugins.xml）：</p>
<pre>&lt;plugin name="VideoPlayer" value="com.dlgreat.bbhtml.plugins.VideoPlayer"/&gt;</pre>
<p>插件的使用代码如下：</p>
<pre>BBGlobalData.validBackKey = false;
window.plugins.videoplayer.play(data, function() {
     console.log('play video ok done !!!');
     setTimeout(function(){BBGlobalData.validBackKey = true;}, 300);
}, function() {
    console.log('play video error done !!!');
    setTimeout(function(){BBGlobalData.validBackKey = true;}, 300);
});</pre>
<p>通过如上代码的修正，即可以在程序中正确处理视频播放的消息回调，插件的待完善之处就是只处理了视频播放成功的消息，默认支持所有的视频格式，可以在java端代码的实现中加入视频文件格式的判断，文件完整性的判断等等，如果有出错的时候，可以利用“error”方法来返回处理结果，具体的参数和“success”一致！</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2012/04/%e5%85%b3%e4%ba%8ephonegap%e8%a7%86%e9%a2%91%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%96%b0/">关于PhoneGap视频插件的代码更新</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2012/04/%e5%85%b3%e4%ba%8ephonegap%e8%a7%86%e9%a2%91%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bb%a3%e7%a0%81%e6%9b%b4%e6%96%b0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PhoneGap 之 Android 端插件开发</title>
		<link>http://blog.zhourunsheng.com/2011/11/phonegap-%e4%b9%8b-android-%e7%ab%af%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91/</link>
		<comments>http://blog.zhourunsheng.com/2011/11/phonegap-%e4%b9%8b-android-%e7%ab%af%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 13:33:53 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[移动开发]]></category>
		<category><![CDATA[PhoneGap]]></category>
		<category><![CDATA[插件]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=1143</guid>
		<description><![CDATA[<p>前面一篇文章 《移动 APP 之跨平台解决方案》介绍了一种跨平台的解决方案，即用开发web app的方式来编写 [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/11/phonegap-%e4%b9%8b-android-%e7%ab%af%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91/">PhoneGap 之 Android 端插件开发</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>前面一篇文章 《<a title="移动 APP 之跨平台解决方案" href="http://blog.zhourunsheng.com/2011/10/%e7%a7%bb%e5%8a%a8-app-%e4%b9%8b%e8%b7%a8%e5%b9%b3%e5%8f%b0%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88/">移动 APP 之跨平台解决方案</a>》介绍了一种跨平台的解决方案，即用开发web app的方式来编写mobile app。鉴于PhoneGap才刚刚新起，还有许多功能因为平台的差异性无法很好的解决，所以我们在实际的开发中，发现有很多功能还需要完善，一种比较好的方式就是编写平台依赖的插件，进而扩展PhoneGap的功能。</p>
<p><img title="lunny" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/11/lunny-300x133.jpg" alt="lunny" width="300" height="133" /><span id="more-1143"></span></p>
<p>本文介绍一下开发和使用插件的一个流程，以 <a href="https://github.com/phonegap/phonegap-plugins/tree/master/Android/VideoPlayer" target="_blank">VideoPlayer</a> 为例。</p>
<ol>
<li>环境搭建，下载 phonegap-android 的源码，下载地址 <a href="https://github.com/phonegap/phonegap-plugins">https://github.com/phonegap/phonegap-plugins</a></li>
<li>编写video.js，提供给web开发端的接口定义，定义了一个VideoPlayer类和play函数，参数为要播放的文件视频地址，代码如下：
<pre>/**
 * 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());
});</pre>
</li>
<li>编写 Android VideoPlayer 的具体实现代码，VideoPlayer/src/com/phonegap/plugins/video/VideoPlayer.java
<pre>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);
    }
}</pre>
</li>
<li>配置插件, res/xml/plugins.xml 添加如下代码
<pre>&lt;plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/&gt;</pre>
</li>
<li>编写代码进行调用,文件开头引入js代码框架，然后进行VideoPlayer类的play函数调用
<pre>&lt;script type="text/javascript" charset="utf-8" src="phonegap.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8" src="video.js"&gt;&lt;/script&gt;

 //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");</pre>
</li>
<li>到此为止，插件的开发和部署，以及调用就都ok了，是不是很简单( ⊙ o ⊙ )啊！</li>
</ol>
<p>最后向大家推荐一本书籍《PhoneGap Beginner's Guide》，相信通过本书的学习，就知道了怎样利用PhoneGap来开发跨平台的mobile app了，同时也可以关注<a href="https://github.com/phonegap">https://github.com/phonegap</a>项目的最新进展情况和新特性，如果可以的话，贡献自己的力量来进行完善和扩充，O(∩_∩)O~！</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/11/phonegap-%e4%b9%8b-android-%e7%ab%af%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91/">PhoneGap 之 Android 端插件开发</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2011/11/phonegap-%e4%b9%8b-android-%e7%ab%af%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://path.to.my/video.mp4" length="151" type="video/mp4" />
		</item>
	</channel>
</rss>
