0

Androidでwebviewを使用してビデオを再生しています。問題は、ビデオが 1 回再生されることです。修正方法についていくつかの回答を見ましたが、まだ機能していません。これが私のコードです:

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

これを試して、showメソッドに追加してください

WebChromeClient.CustomViewCallback CustomViewCallback; mCustomViewCallback = callback; 

次に、hideメソッドで...

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 に答える
0

代替手段を提供したいと思います。完璧ではないかもしれませんが、Web プログラミングの観点から、これに頭を悩ませた後、ビデオを base64 に変換し、それをソースタグにフィードすることが秘訣でした。 (私の場合はjquery)。アセットフォルダーにない場合、混乱することはありません!

于 2013-03-13T15:52:36.740 に答える