0

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

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

2 回答 2

0

我想提供一个替代方案,它可能并不完美,但从 Web 编程的角度来看,在我为此苦苦挣扎了一段时间之后,诀窍是将视频转换为 base64 并将其提供给源标签(在我的情况下是 jquery)。如果它不在资产文件夹中,它就不会混淆!

于 2013-03-13T15:52:36.740 回答
0

试试这个在show方法中添加这个

WebChromeClient.CustomViewCallback CustomViewCallback; mCustomViewCallback = callback; 

然后在隐藏方法中......

mCustomViewCallback.onCustomViewHidden(); mCustomViewCallback = null; HTML5WebView.this.goBack();

编辑 :-

  public class HTML5WebView extends WebView {

    static final String LOGTAG = "HTML5WebView";

    private void init(Context context) {
        mContext = context;     
        Activity a = (Activity) mContext;

        mLayout = new FrameLayout(context);

        mBrowserFrameLayout = (FrameLayout) LayoutInflater.from(a).inflate(R.layout.custom_screen, null);
        mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
        mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.fullscreen_custom_content);

        mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);

        // Configure the webview
        WebSettings s = getSettings();
        s.setBuiltInZoomControls(true);
        s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        s.setUseWideViewPort(true);
        s.setLoadWithOverviewMode(true);
        s.setSaveFormData(true);
        s.setJavaScriptEnabled(true);
        mWebChromeClient = new MyWebChromeClient();
        setWebChromeClient(mWebChromeClient);
        setWebViewClient(new WebViewClient());
        setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        s.setDomStorageEnabled(true); 

        mContentView.addView(this);
    }

    public HTML5WebView(Context context) {
        super(context);
        init(context);
    }

    public HTML5WebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HTML5WebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public FrameLayout getLayout() {
        return mLayout;
    }

    public boolean inCustomView() {
        return (mCustomView != null);
    }

    public void hideCustomView() {
        mWebChromeClient.onHideCustomView();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((mCustomView == null) && canGoBack()){
                goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyWebChromeClient extends WebChromeClient {
        private Bitmap      mDefaultVideoPoster;
        private View        mVideoProgressView;
        FrameLayout frame;

        @Override
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
        {
            HTML5WebView.this.setVisibility(View.GONE);
            isVideoPlaying = true;
            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) {
                callback.onCustomViewHidden();
                return;
            }

            mCustomViewContainer.addView(view);
            mCustomView = view;
            frame = (FrameLayout) mCustomView;
            mCustomViewCallback = callback;
            VideoView mVideoView;
            if(frame.getFocusedChild() instanceof VideoView){
                mVideoView = (VideoView) frame.getFocusedChild();
            }
            mCustomViewContainer.setVisibility(View.VISIBLE);
        }

        @Override
        public void onHideCustomView() {
            if (mCustomView == null)
                return;        

            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);

            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomView);
            mCustomView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewCallback.onCustomViewHidden();
            mCustomViewCallback = null;

            HTML5WebView.this.setVisibility(View.VISIBLE);
            HTML5WebView.this.goBack();

        }
    }
}
于 2013-02-05T05:59:01.410 回答