videojs addEvent在android webview中不起作用

时间:2013-05-17 15:27:09

标签: android webview video.js android-jsinterface

我正在使用video.js实现youtube视频播放器,我在js中动态传递url,并让video.js播放视频。我想做的是让视频完成后让js通知android端,所以我添加了结束事件来调用android方法。但它不起作用,有谁知道这里的问题是什么?

我在android端实现了一个jsinterface“AndroidFunction”

Android方面:

public class MainActivity extends Activity {
/**
 * Called when the activity is first created.
 */
WebView webView;
String ytUrl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.layout1);
    LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    mainLayout.setLayoutParams(params);
    ytUrl = "http://www.youtube.com/watch?v=2NS1umhAAeg";
    webView = new WebView(this);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setPluginsEnabled(true);
    final MyJavaScriptInterface jsInterface = new MyJavaScriptInterface(
            this);
    webView.addJavascriptInterface(jsInterface, "AndroidFunction");
    webView.setWebChromeClient(new WebChromeClient());
    webView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            webView.setClickable(false);
            return false;
        }

    });
    webView.setWebViewClient(new WebViewClient() {
        boolean calledOnce;

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * android.webkit.WebViewClient#onPageFinished(android.webkit.WebView
         * , java.lang.String)
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            if (calledOnce == false) {
                webView.loadUrl("javascript:callFromActivity(\"" + ytUrl
                        + "\")");
            }
            calledOnce = true;
        }
    });

    webView.loadUrl("file:///android_asset/123.html");

    mainLayout.addView(webView);
}

public class MyJavaScriptInterface {
    Context mContext;

    MyJavaScriptInterface(Context c) {
        mContext = c;
    }

    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    public void openAndroidDialog() {
        AlertDialog.Builder myDialog = new AlertDialog.Builder(
                MainActivity.this);
        myDialog.setTitle("Notice");
        myDialog.setMessage("Android side has been called");
        myDialog.setPositiveButton("ON", null);
        myDialog.show();
    }
}
}

在js方面,我实现了一个callAndroid函数来调用android JsInterface“AndroidFunction”,并使用addEvent(“ends”,callAndroid)来调用函数

Js方面:

<script language="javascript">
    var url;

    function callFromActivity(msg){
        url = msg;
        var videoEl = document.createElement("video");
        videoEl.className= "video-js vjs-default-skin";
        videoEl.width = "360";
        videoEl.height = "595";
        videoEl.setAttribute("id", "example_video_1");
        document.getElementById("vid").appendChild(videoEl);

        _V_("example_video_1").ready(function(){

            var myPlayer = this;
            myPlayer.src({ type: "video/youtube", src: url});               
            var mainTrack = myPlayer.values.src;
            var startedOnce = false;
            var endedOnce = false;
            var firstQuartileOnce = false;
            var midpointOnce = false;
            var thirdQuartileOnce = false;





            var callAndroid = function() {
                AndroidFunction.openAndroidDialog();
            }

            myPlayer.addEvent("ended", callAndroid);
        });

    }
    </script>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题

Videojs TypeError: this.addEvent is not a function

尝试: myPlayer.on(“已结束”,callAndroid);