본문 바로가기
옛날

webview 동영상 재생 [Android]

by 차가운게 조아 2015. 12. 17.

안드로이드 웹뷰 동영상 재생하기..





AndroidManifest.xml


<application

        android:hardwareAccelerated="true"

        >


MainActivity.java -> protected void onCreate


myWebView = (WebView) findViewById(R.id.webView);


WebSettings settings = myWebView.getSettings();

settings.setJavaScriptEnabled(true);

settings.setAppCacheEnabled(true);


myWebView.setWebChromeClient(new CustomWebChromeClient());



MainActivity.java


class CustomWebChromeClient extends WebChromeClient {


    @Override

    public void onShowCustomView(View view, CustomViewCallback callback) {


        if (videoCustomView != null) {

            callback.onCustomViewHidden();

            return;

        }


        final FrameLayout frame = ((FrameLayout) view);


        final View v1 = frame.getChildAt(0);

        view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,

                FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));

        v1.setOnKeyListener(new View.OnKeyListener() {


            @Override

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

                    onHideCustomView();

                    return true;

                }

                return false;

            }

        });


        videoCustomView = view;

        customViewContainer.setVisibility(View.VISIBLE);

        customViewContainer.setBackgroundColor(Color.BLACK);

        customViewContainer.bringToFront();

        myWebView.setVisibility(View.GONE);


        customViewContainer.addView(videoCustomView);

    }


    @Override

    public void onHideCustomView() {

        super.onHideCustomView();


        customViewContainer.removeView(videoCustomView);

        videoCustomView = null;

        customViewContainer.setVisibility(View.INVISIBLE);

        myWebView.setVisibility(View.VISIBLE);


    }

}



MainActivity.java


public class MainActivity extends Activity {


    private WebView myWebView;

    private View containerView;

    private FrameLayout customViewContainer;

    private View videoCustomView;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        myWebView = (WebView)findViewById(R.id.myWebView);

        customViewContainer = (FrameLayout) findViewById(R.id.customView_frame);


        WebSettings settings = myWebView.getSettings();

        settings.setJavaScriptEnabled(true);

        settings.setAppCacheEnabled(true);


        myWebView.setWebChromeClient(new CustomWebChromeClient());


        myWebView.loadUrl("");

    }


    class CustomWebChromeClient extends WebChromeClient {


        @Override

        public void onShowCustomView(View view, CustomViewCallback callback) {


            if (videoCustomView != null) {

                callback.onCustomViewHidden();

                return;

            }


            final FrameLayout frame = ((FrameLayout) view);


            final View v1 = frame.getChildAt(0);

            view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,

                    FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));

            v1.setOnKeyListener(new View.OnKeyListener() {


                @Override

                public boolean onKey(View v, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

                        onHideCustomView();

                        return true;

                    }

                    return false;

                }

            });


            videoCustomView = view;

            customViewContainer.setVisibility(View.VISIBLE);

            customViewContainer.setBackgroundColor(Color.BLACK);

            customViewContainer.bringToFront();

            myWebView.setVisibility(View.GONE);


            customViewContainer.addView(videoCustomView);

        }


        @Override

        public void onHideCustomView() {

            super.onHideCustomView();


            customViewContainer.removeView(videoCustomView);

            videoCustomView = null;

            customViewContainer.setVisibility(View.INVISIBLE);

            myWebView.setVisibility(View.VISIBLE);


        }

    }

}



activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:id="@+id/main_frame"

    tools:context=".MainActivity">


    <WebView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:id="@+id/myWebView"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_above="@+id/container"

        android:scrollbars="none" />


    <FrameLayout

        android:id="@+id/customView_frame"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_alignParentRight="true"

        android:layout_alignParentEnd="true"

        android:layout_alignParentBottom="true" />


</RelativeLayout>