ARView.java 7.31 KB
package cn.syar.engine;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.VideoView;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

public class ARView extends GLSurfaceView {
    private VideoRenderer videoRenderer;
    public ARView(Context context){
        super(context);
        init(context);
        ARView.context = context;
    }

    private void init(Context context) {
        setEGLContextFactory(new EGLContextFactory(){
            private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
            @Override
            public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig){
                // Trying to create an OpenGL ES 3.0 context
                int[] attribList = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL10.EGL_NONE};
                EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attribList);
                if ((null == context) || context.equals(EGL10.EGL_NO_CONTEXT)) {
                    attribList[1] = 2;
                    context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attribList);
                }
                return context;
            }

            /**
             * It destroys the OpenGL Context.
             */
            @Override
            public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context){
                egl.eglDestroyContext(display, context);
            }
        });
        ///
        setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser(){
            @Override
            public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display){
                final int EGL_OPENGL_ES2_BIT = 0x0004;
                final int[] attrib = { EGL10.EGL_RED_SIZE, 4, EGL10.EGL_GREEN_SIZE, 4, EGL10.EGL_BLUE_SIZE, 4,
                        EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE };

                int[] num_config = new int[1];
                egl.eglChooseConfig(display, attrib, null, 0, num_config);

                int numConfigs = num_config[0];
                if (numConfigs <= 0)
                    throw new IllegalArgumentException("fail to choose EGL configs");

                EGLConfig[] configs = new EGLConfig[numConfigs];
                egl.eglChooseConfig(display, attrib, configs, numConfigs,
                        num_config);

                for (EGLConfig config : configs)
                {
                    int[] val = new int[1];
                    int r = 0, g = 0, b = 0, a = 0, d = 0;
                    if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_DEPTH_SIZE, val))
                        d = val[0];
                    if (d < 16)
                        continue;

                    if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_RED_SIZE, val))
                        r = val[0];
                    if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_GREEN_SIZE, val))
                        g = val[0];
                    if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_BLUE_SIZE, val))
                        b = val[0];
                    if (egl.eglGetConfigAttrib(display, config, EGL10.EGL_ALPHA_SIZE, val))
                        a = val[0];
                    if (r == 8 && g == 8 && b == 8 && a == 0)
                        return config;
                }
                return configs[0];
            }
        });
        ////设置渲染器
        setRenderer(new GLSurfaceView.Renderer() {
            @Override
            public void onSurfaceCreated(GL10 gl, EGLConfig config) {
                SyARLogger.getInstance().debug("ARView onSurfaceCreated");
                videoRenderer = new VideoRenderer();
            }

            @Override
            public void onSurfaceChanged(GL10 gl, int w, int h) {
                SyARLogger.getInstance().debug("ARView onSurfaceChanged w="+w +" h="+h);
                try {
                    synchronized (this) {
                        videoRenderer.init(w,h);
                    }
                } catch (Throwable ex) {

                }
            }

            @Override
            public void onDrawFrame(GL10 gl) {
                try {
                    synchronized (this) {
                        videoRenderer.draw(null,0,0,0);
                    }
                } catch (Throwable ex) {

                }
            }
        });
    }

    @Override
    protected void onDetachedFromWindow()
    {
        SyARLogger.getInstance().debug("### ARView dispose");
        try {
            synchronized (this) {
                videoRenderer.destroy();
                if(videoView != null){
                    exitFullScreenCallback();
                    videoView = null;
                }
            }
        } catch (Throwable ex) {
        }
        super.onDetachedFromWindow();
    }

    boolean firstTouch = false;
    long startTime = 0;
    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        if(event.getAction() == event.ACTION_DOWN){
            if(firstTouch && (System.currentTimeMillis() - startTime) <= 300) {
                //do stuff here for double tap
                SyARLogger.getInstance().debug(" ###doubleTap### ");
                doubleTap();
                firstTouch = false;
            } else {
                firstTouch = true;
                startTime = System.currentTimeMillis();
                return false;
            }
        }
        return true;
    }


    private static VideoView videoView;
    public static ViewGroup preview;
    public static Context context;
    private static boolean isVideoFullScreen = false;
    public static void fullScreenVideoPlay(final String videoURL){
        SyARLogger.getInstance().debug("###fullScreenVideoPlay "+videoURL);
        UnityActivityHelper.currActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(videoView == null){
                    videoView = new VideoView(context);
                    FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                    layoutParams.gravity = Gravity.CENTER;
                    preview.addView(videoView,layoutParams);
                    MediaControllerEx mediaController = new MediaControllerEx(UnityActivityHelper.currActivity,videoView);
                }
                videoView.setVideoPath(videoURL);
                videoView.requestFocus();
                videoView.start();
                videoView.setZOrderOnTop( true );
                isVideoFullScreen = true;
            }
        });
    }

    public static void exitVideoFullScreen(){
        if(videoView != null) {
            videoView.stopPlayback();
            videoView.clearFocus();
            preview.removeView(videoView);
            videoView = null;
            exitFullScreenCallback();
        }
    }

    private native void doubleTap();
    private static native void exitFullScreenCallback();
}