PhoneGap插件開發-流媒體

PhoneGap插件開發-流媒體 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章