现在的位置: 首页 > 移动开发> 正文
关于PhoneGap视频插件的代码更新
2012年04月13日 移动开发 暂无评论 ⁄ 被围观 5,449+

本文主要介绍PhoneGap视频插件的代码更新,具体插件的开发过程请参照我以前的博文《phonegap之android端插件开发》。现在PhoneGap已经把代码移交到apache开源项目cordova里面,本文代码的更新主要涉及到两项,一是把以前phoneGap的引用迁移到cordova的引用中,二是在原来插件代码的基础上增加回调机制。

以前版本的视频插件可以在android设备上面正常工作,但是有一个潜在的问题,因为插件启动视频播放利用的是 “startActivity”方法,无法获取到视频播放结束的消息,本次代码的主要改进就是改变原来启动视频播放的函数,使用“startActivityForResult”方法,这样当视频播放结束的时候,“onActivityResult”方法会被回调,从而该插件就能正确通知插件使用者视频已正常播放结束,可以继续处理其他流程。

Java 端的完整代码如下:

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

Javascript端的完整代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* 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());
});
/* 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());
});

插件的配置代码如下(xml/plugins.xml):

1
<plugin name="VideoPlayer" value="com.dlgreat.bbhtml.plugins.VideoPlayer"/>
<plugin name="VideoPlayer" value="com.dlgreat.bbhtml.plugins.VideoPlayer"/>

插件的使用代码如下:

1
2
3
4
5
6
7
8
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);
});
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);
});

通过如上代码的修正,即可以在程序中正确处理视频播放的消息回调,插件的待完善之处就是只处理了视频播放成功的消息,默认支持所有的视频格式,可以在java端代码的实现中加入视频文件格式的判断,文件完整性的判断等等,如果有出错的时候,可以利用“error”方法来返回处理结果,具体的参数和“success”一致!

给我留言

留言无头像?


×
腾讯微博