2013-11-14 44 views

回答

2

您可以在WebView上方創建Frame佈局並將其附加到VideoView。通過這種方式,您可以管理大小,添加自定義MediaControls,但是有很多工作,並且您將面臨許多問題。

編輯:您可以捕捉事件:

  • 如果你能處理HTML,你可以添加JS方法,調用應用程序方法throught JS的橋樑。舉例來說,你把一個圖像與onclick事件。在您的應用程序點擊此圖片調用JsVideFrameCallbackImpl.playVideo('videoUrl')http://android-developers.blogspot.com/2008/09/using-webviews.html):

    <img src="img/video_1.jpg" width="494px" height="340px" onclick="javascript: JsVideFrameCallbackImpl.playVideo('videoUrl')"> 
    

,並在您的活動連接addJavascriptInterface到網頁視圖:

wv.addJavascriptInterface(new JsVideFrameCallbackImpl(), "JsVideFrameCallbackImpl"); 

//.... 

private final class JsVideFrameCallbackImpl { 
    private final static String LOG_TAG = "JsVideFrameCallbackImpl"; 

    @SuppressWarnings("unused") 
    @JavascriptInterface 
    public void playVideo(final String videoUri) { 
        // call VideoPlayer activity as example 
     startActivity(new Intent(context, VideoPlayer.class)); 
    } 
} 
  • ,或者你可以得到任何COORDS html元素通過相同的js橋和地方VideoViewFrameLayout(不要忘記啓用js在webview wv.getSettings().setJavaScriptEnabled(true); before using javascript in webview):

    wv.loadUrl("javascript: jsInitMedia()"); // call js method jsInitMedia 
    

jsInitMedia在HTML(或者你可以直接在WebView中應用此代碼:wv.loadUrl("javascript: var el = document.getElemen...")

function jsInitMedia() { 
     var el = document.getElementById(dataArray[i]); var coords = el.getBoundingClientRect(); 
     // get top, left coords, width and height with coords.width, el.offsetTop and so on. Send them back as string (i use json format) 
     JsVideFrameCallbackImpl.init(jsonDataWithCoordsAsString); 
    } 

,並在Java代碼中JsVideFrameCallbackImpl

@JavascriptInterface 
    public void init(final String json) { 
     Log.d(LOG_TAG, "Init:" + json); 
     // parse json and extract left, top, width and height 
        // now you can place any View with corresponding coords on your frame 
        //as well you can easy handle all click events 
    } 
+0

如何我可以捕獲從WebView播放的視頻事件並在VideoView上播放它嗎? –

+0

我要在我的回答中添加對您問題的回答,好嗎? – validcat

+0

我只是將最小的API設置爲11.您的解決方案不實用,因爲我沒有編寫將輸出HMTL的代碼。更不用說,我必須找到一種方法來獲得視頻和圖像本身的圖像不會足夠直觀,以指示嵌入式視頻。 –