示例#1
0
    // Start is called before the first frame update
    void Start()
    {
        // 添加监听
        inputField.onEndEdit.AddListener(TextFieldOnEndEdit);
        prepareBtn.onClick.AddListener(Prepare);
        playBtn.onClick.AddListener(Play);
        pauseBtn.onClick.AddListener(Pause);
        stopBtn.onClick.AddListener(Stop);
        clearBtn.onClick.AddListener(ClearLog);

        progressSlider.minValue = 0.0f;
        progressSlider.value    = 0.0f;
        progressSlider.GetComponent <SliderDrag>().EndDrag = progressSliderValueDidChange;


        volumeSlider.minValue = 0.0f;
        volumeSlider.maxValue = 1.0f;
        volumeSlider.value    = 1.0f;
        volumeSlider.GetComponent <SliderDrag>().EndDrag = volumeSliderValueDidChange;


        float width  = displayLayer.rectTransform.rect.width;
        float height = displayLayer.rectTransform.rect.height;

        mVideoTexture            = new Texture2D((int)width, (int)height, TextureFormat.RGB565, false);
        mVideoTexture.filterMode = FilterMode.Bilinear;
        mVideoTexture.wrapMode   = TextureWrapMode.Clamp;

        displayLayer.texture = mVideoTexture;

        //// 初始化播放器
        mVideoPlayer = gameObject.AddComponent <VideoPlayerHelper>();
        mVideoPlayer.Init();

        // 注意纹理的获取时机,mVideoTexture被负载到gameobject才能获取到texturePtr
        texPtr = mVideoTexture.GetNativeTexturePtr();
        mVideoPlayer.SetVideoTexturePtr(texPtr);


        HandleStateChange(VideoPlayerHelper.MediaState.NOT_READY);
        mCurrentState = VideoPlayerHelper.MediaState.NOT_READY;
    }
示例#2
0
    private IEnumerator PrepareVideo()
    {
        // Get the video player status
        VideoPlayerHelper.MediaState state = mVideoPlayer.GetStatus();

        if (state == VideoPlayerHelper.MediaState.ERROR)
        {
            Debug.Log("Cannot prepare video, as the player is in error state.");
            HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
            this.enabled = false;
        }
        else
        {
            // Not in error state, we can move on...
            while (mVideoPlayer.GetStatus() == VideoPlayerHelper.MediaState.NOT_READY)
            {
                // Wait one or few frames for video state to become ready
                yield return(new WaitForEndOfFrame());
            }

            // Video player is ready
            Debug.Log("VideoPlayer ready.");

            // Initialize the video texture
            bool isOpenGLRendering = (
                VuforiaRenderer.Instance.GetRendererAPI() == VuforiaRenderer.RendererAPI.GL_20 ||
                VuforiaRenderer.Instance.GetRendererAPI() == VuforiaRenderer.RendererAPI.GL_30);
            InitVideoTexture(isOpenGLRendering);

            // Can we play this video on a texture?
            isPlayableOnTexture = mVideoPlayer.IsPlayableOnTexture();

            if (isPlayableOnTexture)
            {
                // Pass the video texture id to the video player
                mVideoPlayer.SetVideoTexturePtr(mVideoTexture.GetNativeTexturePtr());

//                // Get the video width and height
//                int videoWidth = mVideoPlayer.GetVideoWidth();
//                int videoHeight = mVideoPlayer.GetVideoHeight();
//
//                if (videoWidth > 0 && videoHeight > 0)
//                {
//                    // Scale the video plane to match the video aspect ratio
//                    float aspect = videoHeight / (float)videoWidth;
//
//                    // Flip the plane as the video texture is mirrored on the horizontal
//                    transform.localScale = new Vector3(-0.1f, 0.1f, 0.1f * aspect);
//                }

                // Seek ahead if necessary
                if (mSeekPosition > 0)
                {
                    mVideoPlayer.SeekTo(mSeekPosition);
                }
            }
            else
            {
                // Handle the state change
                state = mVideoPlayer.GetStatus();
                HandleStateChange(state);
                mCurrentState = state;
            }

            // Scale the icon
            ScaleIcon();

            mIsInited = true;
        }

        mInitInProgess = false;
        yield return(new WaitForEndOfFrame());
    }
    void OnRenderObject()
    {
        if (mAppPaused)
        {
            return;
        }

        if (!mIsInited)
        {
            bool isMetalRendering = (VuforiaRenderer.Instance.GetRendererAPI() == VuforiaRenderer.RendererAPI.METAL);
            // Initialize the video player
            if (mVideoPlayer.Init(isMetalRendering) == false)
            {
                Debug.Log("Could not initialize video player");
                HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
                this.enabled = false;
                return;
            }

            // Load the video
            if (mVideoPlayer.Load(m_path, mMediaType, false, 0) == false)
            {
                Debug.Log("Could not load video '" + m_path + "' for media type " + mMediaType);
                HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
                this.enabled = false;
                return;
            }

            // Successfully initialized
            mIsInited = true;
        }
        else if (!mIsPrepared)
        {
            // Get the video player status
            VideoPlayerHelper.MediaState state = mVideoPlayer.GetStatus();

            if (state == VideoPlayerHelper.MediaState.ERROR)
            {
                Debug.Log("Could not load video '" + m_path + "' for media type " + mMediaType);
                HandleStateChange(VideoPlayerHelper.MediaState.ERROR);
                this.enabled = false;
            }
            else if (state < VideoPlayerHelper.MediaState.NOT_READY)
            {
                // Video player is ready

                // Initialize the video texture
                bool isMetalRendering = (VuforiaRenderer.Instance.GetRendererAPI() == VuforiaRenderer.RendererAPI.METAL);
                InitVideoTexture(isMetalRendering);

                // Can we play this video on a texture?
                isPlayableOnTexture = mVideoPlayer.IsPlayableOnTexture();

                if (isPlayableOnTexture)
                {
                    // Pass the video texture id to the video player
                    // TODO: GetNativeTexturePtr() call needs to be moved to Awake method to work with Oculus SDK if MT rendering is enabled
                    mVideoPlayer.SetVideoTexturePtr(mVideoTexture.GetNativeTexturePtr());

                    // Get the video width and height
                    int videoWidth  = mVideoPlayer.GetVideoWidth();
                    int videoHeight = mVideoPlayer.GetVideoHeight();

                    if (videoWidth > 0 && videoHeight > 0)
                    {
                        // Scale the video plane to match the video aspect ratio
                        float aspect = videoHeight / (float)videoWidth;

                        // Flip the plane as the video texture is mirrored on the horizontal
                        transform.localScale = new Vector3(-0.1f, 0.1f, 0.1f * aspect);
                    }

                    // Seek ahead if necessary
                    if (mSeekPosition > 0)
                    {
                        mVideoPlayer.SeekTo(mSeekPosition);
                    }
                }
                else
                {
                    // Handle the state change
                    state = mVideoPlayer.GetStatus();
                    HandleStateChange(state);
                    mCurrentState = state;
                }

                // Scale the icon
                ScaleIcon();

                // Video is prepared, ready for playback
                mIsPrepared = true;
            }
        }
        else
        {
            if (isPlayableOnTexture)
            {
                // Update the video texture with the latest video frame
                VideoPlayerHelper.MediaState state = mVideoPlayer.UpdateVideoData();
                if ((state == VideoPlayerHelper.MediaState.PLAYING) ||
                    (state == VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN))
                {
                    GL.InvalidateState();
                }


                // Check for playback state change
                if (state != mCurrentState)
                {
                    HandleStateChange(state);
                    mCurrentState = state;
                }
            }
            else
            {
                // Get the current status
                VideoPlayerHelper.MediaState state = mVideoPlayer.GetStatus();
                if ((state == VideoPlayerHelper.MediaState.PLAYING) ||
                    (state == VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN))
                {
                    GL.InvalidateState();
                }

                // Check for playback state change
                if (state != mCurrentState)
                {
                    HandleStateChange(state);
                    mCurrentState = state;
                }
            }
        }

        CheckIconPlaneVisibility();
    }