Log in or register to post comments

VideoPlayer with Image Target

July 27, 2017 - 1:33pm #1

Hey, I have a VideoPlayer that is playing a video from an online link. I am trying to make the application so that when a specific image is tracked a video will start to play. Currently I am trying to play a video that is already prepared and paused in the Start() function. I have changed the code many times and nothing is working. can someone tell me what is wrong with the code please?

using UnityEngine;

using UnityEngine.Video;

namespace Vuforia

{

    /// <summary>

    /// A custom handler that implements the ITrackableEventHandler interface.

    /// </summary>

    public class DefaultTrackableEventHandler : MonoBehaviour,

                                                ITrackableEventHandler

    {

        #region PRIVATE_MEMBER_VARIABLES



        private TrackableBehaviour mTrackableBehaviour;

        private VideoPlayer vPlayer;

        #endregion // PRIVATE_MEMBER_VARIABLES

 

        #region UNTIY_MONOBEHAVIOUR_METHODS

        void Start()

        {

            mTrackableBehaviour = GetComponent<TrackableBehaviour>();

            if (mTrackableBehaviour)

            {

                mTrackableBehaviour.RegisterTrackableEventHandler(this);

            }

            GameObject camera = GameObject.Find("ARCamera/Camera");

            vPlayer = camera.AddComponent<VideoPlayer>();

            vPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

            vPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;

            vPlayer.playOnAwake = false;

            //vPlayer.targetCameraAlpha = 0.5f;

            //vPlayer.prepareCompleted += Prepared;

            vPlayer.Pause();

           

            Debug.Log("prep reached!");

           

        }

        void Playvid()

        {

            vPlayer.Play();

            Debug.Log("playing vid!");

        }

        // void Prepared(UnityEngine.Video.VideoPlayer vPlayer)

        // {

        //    Debug.Log("End reached!");

        //    vPlayer.Play();

        //}

        #endregion // UNTIY_MONOBEHAVIOUR_METHODS

        #region PUBLIC_METHODS

        /// <summary>

        /// Implementation of the ITrackableEventHandler function called when the

        /// tracking state changes.

        /// </summary>

        public void OnTrackableStateChanged(

                                        TrackableBehaviour.Status previousStatus,

                                        TrackableBehaviour.Status newStatus)

        {

            if (newStatus == TrackableBehaviour.Status.DETECTED ||

                newStatus == TrackableBehaviour.Status.TRACKED ||

                newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)

            {

                OnTrackingFound();

            }

            else

            {

                OnTrackingLost();

            }

        }

        #endregion // PUBLIC_METHODS

        #region PRIVATE_METHODS



        private void OnTrackingFound()

        {

            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);

            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

            // Enable rendering:

            foreach (Renderer component in rendererComponents)

            {

                component.enabled = true;

            }

            // Enable colliders:

            foreach (Collider component in colliderComponents)

            {

                component.enabled = true;

            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");

            if (mTrackableBehaviour.TrackableName == "design")

            {

                Playvid();

                Debug.Log("design seen!");

            }

        }

     



        private void OnTrackingLost()

        {

            Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);

            Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

            // Disable rendering:

            foreach (Renderer component in rendererComponents)

            {

                component.enabled = false;

            }

            // Disable colliders:

            foreach (Collider component in colliderComponents)

            {

                component.enabled = false;

            }

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");

        }

        #endregion // PRIVATE_METHODS

    }

}

VideoPlayer with Image Target

March 9, 2018 - 9:52am #2

Did you get this to work?

Log in or register to post comments