在Android WebView中启用HTML5视频播放? [英] Enabling HTML5 video playback in android WebView?

查看:506
本文介绍了在Android WebView中启用HTML5视频播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的webview应用程序,该应用程序正在使用HTML5视频播放器显示充满短视频剪辑的网站。在默认的安卓网页浏览器中,一切都运行正常,但webview不会播放任何视频剪辑。

I am building a simple webview application which is now displaying a website filled with short video clips, using the HTML5 video player. Everything runs ok in the default android web browser but webview wont't play any of the video clips.

用于播放视频剪辑的Html代码如下: / p>

The Html code used to play the video clips is as follows:

<video poster preload="true" controls autoplay width="500" height="200">
  <source src="http://www.edmondvarga.com/demo/videos/video.mp4" type="video/mp4">
  </video>

主要Activity.java:

Main Activity.java :

package tscolari.mobile_sample;

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

public class InfoSpotActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mainWebView.loadUrl("http://server.info-spot.net");
    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

如何启用视频播放在webview里面?

How could I enable video playback inside webview?

推荐答案

我从之前的项目中得知,您需要使用 WebChromeClient 可以播放HTML5视频。 (这也为您提供硬件加速支持 - 如果您还在活动中设置了标记)。

I know from a previous project we did that you need to use the WebChromeClient to get HTML5 video to play. (And this also gives you hardware accelerated support too - providing you also set the flags on your activity).

使用:

        mainWebView.setWebChromeClient(new WebChromeClient());

在设置setWebViewClient之前,您可以重写WebChromeClient以拦截您需要处理的任何事件。

Put that before you set the setWebViewClient. You can override the WebChromeClient to intercept any events you need to handle.

然后在您的活动定义中的AndroidManifest.xml中添加:

And in your AndroidManifest.xml within your activity definition, add:

android:hardwareAccelerated="true"

以下引用来自此 SDK页面(向下滚动至HTML5视频支持) p>

The following quote is from this SDK Page (scroll down to HTML5 Video support)


为了支持应用程序中的内嵌HTML5视频,您需要打开硬件加速并设置WebChromeClient。

In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.

这篇关于在Android WebView中启用HTML5视频播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆