我无法在Android网页视图上播放视频.
05-01 12:31:16.092: E/MediaResourceGetter(17241): Unable to read file: file:///android_asset/MediaBook2%20(2)/2B952499A0E681.mp4
每当我按下播放按钮时,我都会收到以下错误
05-01 12:31:23.680: E/chromium(17241): [ERROR:webmediaplayer_android.cc(328)] Not implemented reached in virtual void content::WebMediaPlayerAndroid::setRate(double)
05-01 12:31:23.710: E/MediaPlayer(17241): error (1,-2147483648)
05-01 12:31:23.710: E/MediaPlayer(17241): Error (1,-2147483648)
能够加载任何远程视频并运行,但问题是我从资源文件夹加载本地视频
用于加载文件和设置Web视图的代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_webview);
mContentView = (LinearLayout) findViewById(R.id.linearlayout);
// Keep the webview setup ready
setupWebView();
}
public void setupWebView()
{
webView = (WebView) findViewById(R.id.webView);
// progressBar = (ProgressBar) findViewById(R.id.progressBarForWebView);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setSoundEffectsEnabled(true);
webView.setWebViewClient(new SLCWebViewClient());
webView.setWebChromeClient(new WebChromeClient());
loadContentsInWebView();
}
public void loadContentsInWebView()
{
String localURL = "file:///android_asset/MediaBook2 (2)/SampleForVideo.html";
logger.debug("WebView URL: {}",localURL);
try {
webView.loadUrl(localURL);
}
catch (Exception e) {
e.printStackTrace();
logger.error("Error while loading url",e);
}
}
private class SLCWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url)
{
view.setWebChromeClient(new WebChromeClient()
{
private View mCustomView;
@Override
public void onShowCustomView(View view,WebChromeClient.CustomViewCallback callback)
{
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
// Add the custom view to its container.
mCustomViewContainer.addView(view,COVER_SCREEN_GRAVITY_CENTER);
mCustomView = view;
mCustomViewCallback = callback;
// hide main browser view
mContentView.setVisibility(View.GONE);
// Finally show the custom view container.
mCustomViewContainer.setVisibility(View.VISIBLE);
mCustomViewContainer.bringToFront();
}
});
webView.loadUrl(url);
return true;
}
Sample for Video.HTML代码
干杯,
Saurav
最佳答案
file:/// android_asset协议是特定于WebView的东西.也就是说:其他系统组件无法读取这些URL.
MediaResourceGetter不使用WebView的网络堆栈,因此不“理解”file:/// android_asset协议.
在你提到的另一个问题中,你使用的是本地http服务器 – 尝试从中提供.mp4.