如何使用android在webview中播放视频?

发布于 2024-10-17 09:44:44 字数 287 浏览 2 评论 0原文

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

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

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

I loaded a local html in a 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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2024-10-24 09:44:44

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

然后......当我不得不面对这个问题时,我必须进行一些研究并找到一些有用的答案。基本上,您必须以本机浏览器的方式打开视频

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项目源码

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

问候

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;
    }
}

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.

Regards

又怨 2024-10-24 09:44:44

这应该可以在您已经使用的 2.x 版本中运行。但 doc 表示该标签在浏览器全屏时有效。

您的网络视图也有可能支持它,但它需要全屏。
试试这个。
(不过,我还没有尝试过)

编辑:要使视图全屏显示,您可以尝试以下操作:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

This is supposed to be working in 2.x versions which you're already using. but the doc says that tag will work when browser is fullscreen.

Possibilities are there that your webview will also support it, but it needs to be full screen.
Try this out.
(I haven't tried this, though)

EDIT: To make the view go fullscreen, you may try this:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文