Android WebView Playing HTML5/h.264/mp4 Video, How to get at the MediaPlayer
我有一个..。我有一个里面。在其中,有几个回调旨在返回处理视频位的回调。例如:Activity
WebView
WebChromeClient
MediaPlayer
@Override
public void onPrepared(MediaPlayer mp) {
Log.i(TAG, " -------------> onPrepared");
}
当我将MP4流加载到使用HTML标签(通过注入)时,这些无法触发。WebView
<video>
当我进行活动时,logcat 会报告以下内容:finish()
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
<video>
WebChromeClient
MediaPlayer
MediaPlayer
<video>
WebView
--更新
下面是用于初始化 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);
“帧”代码是用于启动某些视频的iframe(至少Vimeo和YouTube似乎都采用了这种方法)。我修剪了一下,以避免碎屑:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<iframe src="-target.url.with.params-" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
</div>
</body>
</html>
在该 WebView 类中,存在以下类:
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得到一些回应:
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// Jump up and down because we got a mp!
// never happens, so no jumping
}
顺便说一句,我确实使用反射四处挖掘,并以这种方式掌握了MediaPlayer,但是在做那种事情之后,我总是觉得有点不知所措。即使我有MediaPlayer对象,我也不愿意只是发布它。对MP源代码的一些研究表明,不会发生任何不好的事情,但是......
我注意到,这并不是每个设备上都会发生。例如,HTC似乎比三星表现得更好(“相同”的操作系统/API级别 - 我没有足够的雄心来比较两个源树)。
下面提到的行为很烦人,但只对我(谁在看logcat输出。对于用户或应用程序,它似乎没有任何效果。只是我真的很不喜欢这样一种想法,即我正在启动一个MediaPlayer实例,将其设置为忙碌缓存,然后(如果/当用户终止我的应用程序时)让那个丁格尔贝利挂在那里。在这一点上,我有很多钱花在治疗上,这似乎无济于事。
提前致谢。