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

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

问题描述

我正在构建一个简单的 webview 应用程序,它现在使用 HTML5 视频播放器显示一个充满短视频剪辑的网站.在默认的 android 网络浏览器中一切正常,但 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代码如下:

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 视频支持)

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天全站免登陆