如何使用android在webview中播放视频? [英] How to play a video in a webview with android?

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

问题描述

我在 sdcard 中加载了一个本地 html,在这个 html 中我使用了标签:

I loaded a local html in sdcard, and in this html I used tag :

<video id="myvideo" controls width="120" height="60" poster="img/img01.jpg" src="video/01.mp4"></video>

然后我发现我没有加载这个 html,当我禁用标签时:,html 工作正常,我在我的 android avd(2.2) 中测试了这个?

and then I found that I didn't loaded this html, when I disabled the tag:, the html was working fine, and I tested this in my android avd(2.2) ?

推荐答案

首先关心编码.这是一篇文章工作示例和一些为 Android webkit 编码视频的指南.

First of all care the encoding. Here it's an article with a working example and some guidelines to encode videos for Android webkit.

然后......当我不得不面对这个问题时,我不得不研究了一下并找到了一些有用的答案.基本上你必须像原生浏览器那样打开视频

And then... when I had to face this issue, I had to research a bit and found some useful answers. Basically you have to open the video the way the native browser does

public class InredisChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener {
    private InterfazWebInredis interfazWeb; // Use Your WebView instance instead

    private VideoView mCustomVideoView;

    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private LinearLayout mErrorConsoleContainer;
    static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER);

    public InredisChromeClient(InterfazWebInredis iwi) {
        super();
        this.interfazWeb = iwi;
    }

    public void onShowCustomView(View view, CustomViewCallback callback) {
        // super.onShowCustomView(view, callback);
        if (view instanceof FrameLayout) {
            mCustomViewContainer = (FrameLayout) view;
            mCustomViewCallback = callback;
            mContentView = (LinearLayout) interfazWeb.findViewById(R.id.mainContainer);
            if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
                mCustomVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
                // frame.removeView(video);
                mContentView.setVisibility(View.GONE);
                mCustomViewContainer.setVisibility(View.VISIBLE);
                interfazWeb.setContentView(mCustomViewContainer);
                mCustomVideoView.setOnCompletionListener(this);
                mCustomVideoView.setOnErrorListener(this);
                mCustomVideoView.start();
            }
        }
    }

    public void onHideCustomView() {
        if (mCustomVideoView == null)
            return;
        // Hide the custom view.
        mCustomVideoView.setVisibility(View.GONE);
        // Remove the custom view from its container.
        mCustomViewContainer.removeView(mCustomVideoView);
        mCustomVideoView = null;
        mCustomViewContainer.setVisibility(View.GONE);
        mCustomViewCallback.onCustomViewHidden();
        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.stop();
        mCustomViewContainer.setVisibility(View.GONE);
        onHideCustomView();
        interfazWeb.setContentView(mContentView);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        interfazWeb.setContentView(R.layout.main);
        return true;
    }
}

所以,这段代码的灵感来自于 浏览器的android项目源码.

So, this code is much inspired on the android project source code of the browser.

好吧,这种行为是全屏打开视频.我不知道是否可以在网页内的自己的框架中播放视频.但是这个解决方案对我有用,我希望你也能.

And well, the behaviour of this is opening the video full-screen. I don't know if it's possible to play the video in its own frame within the webpage. But this solution did the trick for me, I hope for you too.

问候

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

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