无法在Web View中播放视频

时间:2014-05-01 06:59:00

标签: android android-layout android-webview

我无法在Android网络视图上播放视频。

我已将html和视频文件保存在我的资源文件夹中。

每当我加载html文件时,它都会给我错误

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)

能够加载任何远程视频并运行,但问题是当我从assets文件夹加载本地视频时 用于加载文件和设置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代码

    <!DOCTYPE html>
<html>
<title>Testing for Video</title>
<body>

<video width="320" height="240" controls>
  <source src="2B952499A0E681.mp4">
</video>

</body>
</html>

布局文件的代码

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
      android:id="@+id/fullscreen_custom_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FF000000"/>

<LinearLayout 
      android:id="@+id/linearlayout"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

 <WebView
       android:id="@+id/webView"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />

欢呼声, Saurav

3 个答案:

答案 0 :(得分:2)

file:///android_asset协议是WebView特有的。也就是说:其他系统组件无法读取这些URL。

MediaResourceGetter没有使用WebView的网络堆栈,因此无法理解&#34; file:///android_asset协议。

在你提到的另一个问题中,你使用了本地的http服务器 - 尝试从中提供.mp4。

答案 1 :(得分:2)

感谢Marcin的回答。

我可以通过加载视频来运行html文件。 我的问题是我正在使用/ MediaBook2(2)/SampleForVideo.html。但是从资产加载时应删除'/'。我通过修剪'/'来分割字符串并且它有效。

但这只是我正在努力澄清理解的一个示例场景。

我有一个更大的文件夹结构,现在最终加载了.mp4文件。

播放媒体播放器,但播放器没有播放任何文件。

答案 2 :(得分:0)

如果在2018年的Android webview上播放视频时仍然存在问题,请让我们有机会尝试以下代码。

爪哇:

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends Activity {
        private WebView webview;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            webview = new WebView(this);
            setContentView(webview);

            final WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setPluginState(WebSettings.PluginState.ON);

            webview.setWebViewClient(new WebViewClient() {
                // autoplay when finished loading via javascript injection
                public void onPageFinished(WebView view, String url) { 
                    webview.loadUrl("javascript:(function() { 
                        document.getElementsByTagName('video')[0].play(); 
                    })()"); 
                }
            });
            webview.setWebChromeClient(new WebChromeClient());

            webview.loadUrl("http://html5demos.com/video");
        }

        @Override
        protected void onPause() {
            super.onPause();
            webview.onPause();
        }

        @Override
        protected void onResume() {
            webview.onResume();
            super.onResume();
        }
    }

布局:

    <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="your.package.com">

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />

        <application
            android:hardwareAccelerated="true"
            android:allowBackup="false"
            android:icon="@mipmap/logo_example"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/logo_example"
            android:supportsRtl="true"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

参考文献: https://gist.github.com/aprock/5913322