机器人的WebView播放HTML5 / H.264 / MP4视频,如何获得在MediaPlayer的机器人、如何获得、视频、WebView

2023-09-06 13:10:12 作者:三年、

我有一个活动这是一个的WebView 。我有一个 WebChromeClient 里面。这里面,有这意味着可以返回的MediaPlayer 处理视频的比特数回调。例如:

I have an Activity which is a WebView. I have a WebChromeClient inside it. Inside that, there are several callbacks which are meant to return the MediaPlayer handling the video bits. For example:

@Override
public void onPrepared(MediaPlayer mp) {
    Log.i(TAG, " -------------> onPrepared");
}

这些不火,当我加载一个MP4流进的WebView 使用HTML <视频> 标签(通过注射)。

These fail to fire when I load an MP4 stream into the WebView using HTML <video> tags (via injection).

当我完成()的活动中,logcat的报告这样的:

When I finish() the activity, the logcat reports this:

09-13 23:55:24.590: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.
09-13 23:55:24.680: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.
09-13 23:55:24.680: E/MediaPlayer(7949): mOnVideoSizeChangedListener is null. Failed to send MEDIA_SET_VIDEO_SIZE message.
09-13 23:55:25.675: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.
09-13 23:55:26.735: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.
09-13 23:55:27.755: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.
09-13 23:55:28.705: E/MediaPlayer(7949): mOnBufferingUpdateListener is null. Failed to send MEDIA_BUFFERING_UPDATE message.

尝试,因为我可以,我不能让它停下来,即使的WebView 被清零,然后销毁。当使用加载视频&LT;视频&GT; 标签,我不知道有什么办法强迫 WebChromeClient 来使用特定的MediaPlayer ,我为它创建。它似乎决心要使用一些隐藏的一个,这报告上面。有没有办法找到的MediaPlayer 通过从创建一个&LT;视频&GT; 在的WebView ?

Try as I may, I can't get it to stop, even though the WebView is cleared, and then destroyed. When loading video using <video> tags, I don't know of any way to force the WebChromeClient to use a particular MediaPlayer that I create for it. It seems determined to use some hidden one, which reports the above. Is there a way to locate the MediaPlayer created from via a <video> tag on a WebView?

- 更新

下面是code初始化web视图:

Here is the code for initializing the WebView:

        mWebView = new WebView(mContext);

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginState(PluginState.OFF);
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.clearHistory();
        mWebView.clearFormData();
        mWebView.clearCache(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setUserAgentString("Android Mozilla/5.0 AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");

        chrome = new MyWebChromeClient();
        mWebView.setWebChromeClient(chrome);

        wvc = new MyWebViewClient();
        mWebView.setWebViewClient(wvc);

        mDomain = "http://foo.bar.com";
        mWebView.requestFocus(View.FOCUS_DOWN);

        String meat = genMainHTML(R.raw.frame);

        mWebView.loadDataWithBaseURL(mDomain, meat, "text/html", "utf-8", null);

帧code是一个iframe推出一些视频(包括Vimeo的和YouTube似乎采取这种做法,至少)。我已经修剪这一点,以避免冗余代码:

The "frame" code is an iframe to launch some video (both Vimeo and YouTube seem to adopt this approach, at least). I have pruned this a bit to avoid cruft:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
     <iframe src="-target.url.with.params-" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
     </iframe>
</div>
</body>
</html>

在那的WebView类中,存在此类:

Within that WebView class, there exists this class:

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String injection = injectPageMonitor();
            if(injection != null && !injectionComplete) {
                // Log.i(TAG, " ---------------> Page Loaded . . .");
                view.loadUrl(injection);
                injectionComplete = true;
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.d(TAG, "*** Error ["+description+"] ["+failingUrl+"]");
        Toast.makeText(mContext, description, Toast.LENGTH_SHORT).show();
    }

}

和也这个类:

    private class MyWebChromeClient extends WebChromeClient implements MediaPlayer.OnInfoListener, MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnErrorListener, MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnBufferingUpdateListener {

       @Override
       public void onProgressChanged(WebView view, int progress) {
           //Log.e(TAG, " -------------> Progress Changed . . . . ["+progress+"] mWebView ["+mWebView+"] ["+view+"]");
           if(progress == 100) {
                               // Do something really interesting
           }  else  {
               updateBuffering(UPDATE_PERCENT_LAUNCH_EXTRACTOR + (progress / 2));
           }
       }

       @Override
       public boolean onConsoleMessage(ConsoleMessage cm) {
                       // Spit out lots of console messages here
           return(true);
       }
   }

和我所有的秋后算账覆盖,以防万一我很幸运,并得到了一些响应出的MediaPlayer的:

And I have all the usual suspects overridden, just in case I get lucky and get some response out of the MediaPlayer:

        @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // Jump up and down because we got a mp!
                    // never happens, so no jumping
    }

作为一个侧面说明,我使用反射也挖过来,并得到阿霍德在MediaPlayer这样的,但我一直在做这样的事之后,觉得有点ookey。而且即使我有MediaPlayer对象,我不愿意,只是将其释放。小源$ C ​​$下MP研究表明,没什么不好会发生,但。 。

As a side-note, I did dig around using reflection and got ahold of the MediaPlayer that way, but I always feel a bit ookey after doing that kind of thing. And even though I have the MediaPlayer object, I'm reluctant to just release it. A little studying of the source code for MP suggests that nothing bad would happen, but . . .

这不会发生在每个设备上,我已经注意到了。 HTC似乎表现优于三星为例。(同OS / API级别 - 我真的不足够的雄心来比较两个源树)

This does not happen on every device, I have noticed. HTC seems to behave better than Samsung, for example ("same" OS/API level -- I'm not really ambitious enough to compare the two source trees).

行为下面说明的是烦人,但仅限于我(谁是看logcat的输出,对于用户或应用程序似乎没有任何效果。这只是我真的不喜欢说我踢了一个MediaPlayer的概念例如,将其设置忙于缓存走,然后(如果/当用户终止我的应用程序)离开该dingleberry挂在那里。我有很多的绑在治疗在这一点钱,它似乎并没有得到帮助

The behavior noted below is annoying, but only for me (who is watching the logcat output. For the user or application it seems to have no effect. It's just that I really dislike the notion that I'm kicking off a MediaPlayer instance, setting it busily caching away, and then (if/when the user terminates my app) leaving that dingleberry hanging out there. I have a lot of money tied up in therapy at this point, and it doesn't seem to be helping.

在此先感谢。

推荐答案

有没有办法访问的MediaPlayer,而不反思 - 在WebViewClient和WebChromeClient不要刻意暴露。而你是正确地说,使用反射可能会导致你打破你的应用程序或web视图为code可以在Android的版本各不相同,甚至跨越原始设备制造商。

There is no way to access the MediaPlayer without reflection - the WebViewClient and WebChromeClient do not intentionally expose it. And you are correct in saying that using reflection could cause you to break your app or the WebView as the code can vary across Android versions, and even across OEMs.

如果你想做到这一点,你必须找到HTML5VideoView的对应于您的视频元素的实例。此对象创建并维护一个实例来在MediaPlayer为MPLAYER。请参阅https://github.com/android/platform_frameworks_base/blob/jb-mr2-release/core/java/android/webkit/HTML5VideoView.java.

If you would like to do this, you will have to locate the instance of HTML5VideoView that corresponds to your video element. This object creates and maintains an instance to the MediaPlayer as "mPlayer". Please see https://github.com/android/platform_frameworks_base/blob/jb-mr2-release/core/java/android/webkit/HTML5VideoView.java.

 
精彩推荐
图片推荐