Android Webview仅播放一次视频 [英] Android webview is playing video only once

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

问题描述

我正在使用android中的webview播放视频.问题是视频只播放一次.我已经看到了有关如何解决它的一些答案,但是仍然无法正常工作.这是我的代码:

I am using webview in android to play a video. The problem is that video is playing once. I have seen some answers about how to fix it, but still not working. Here's my code:

public class MyChromeClient extends WebChromeClient implements
    OnCompletionListener, OnErrorListener {

    private Activity _activity;
    private VideoView mCustomVideoView;

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

    public MyChromeClient(Activity context) {
        super();
        _activity = context;
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        if (view instanceof FrameLayout) {
            FrameLayout frame = (FrameLayout) view;
            if (frame.getFocusedChild() instanceof VideoView) {
                mCustomVideoView = (VideoView) frame.getFocusedChild();
                frame.removeView(mCustomVideoView);
                _activity.setContentView(mCustomVideoView);
                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);
        mCustomVideoView.stopPlayback();
        mCustomViewCallback.onCustomViewHidden();
        // Show the content view.
        mContentView.setVisibility(View.VISIBLE);
    }

    public void onCompletion(MediaPlayer mp) {
        //Intent intent = new Intent(_activity, _activity.getClass());
        //intent.setClass(_activity, _activity.getClass());
        //_activity.startActivity(intent);
        //_activity.finish();
    }

    public boolean onError(MediaPlayer mp, int what, int extra) {
        return true;
    }
}

推荐答案

我想提供一种替代方法,它可能并不完美,但是从Web编程的角度来看,我对此提出质疑已有一段时间,诀窍是将视频隐藏到base64并将其提供给source标签(在我的情况下为jquery).如果它不在资产文件夹中,就不会造成混淆!

I would like to offer an alternative, it may not be perfect, but from a web programming point of view, after beating my head against this for some time, the trick was to covert the video to base64 and the feed it to the source tag (jquery in my case). If it isn't in the assets folder it can't get confuse!

这篇关于Android Webview仅播放一次视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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