Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Video plays before Vuforia image target recognition

Discussion in 'Scripting' started by ggorby, Jan 7, 2019.

  1. ggorby

    ggorby

    Joined:
    Aug 29, 2018
    Posts:
    2
    I noticed that a video placed on a quad that is a child of a Vuforia image target starts playing immediately. I found a very detailed tutorial that addresses this specific issue by editing the DefaultTrackableEventHandler script. Unfortunately, the changes to the script generate errors in my development setting. The tutorial was done with Unity3D 2017.3 and Vuforia 7. I am using 2018.3.0f2 personal version and Vuforia 7.5.26. This issue was addressed at time 32:05 in the following video: https://youtu.be/4W5e6-TSpW0
    I have done my due diligence looking at the core samples including the fissure image target in the 3-ImageTargets scene. I have Googled the topic to death, and can't get it working. I am including the script from the YouTube tutorial in case someone with experience can spot what needs to be changed. I would be incredibly grateful for the help!
    Code (CSharp):
    1. /*==============================================================================
    2. Copyright (c) 2017 PTC Inc. All Rights Reserved.
    3.  
    4. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    5. All Rights Reserved.
    6. Confidential and Proprietary - Protected under copyright and other laws.
    7. ==============================================================================*/
    8.  
    9. using UnityEngine;
    10. using Vuforia;
    11.  
    12. // need to import video functionality
    13. using UnityEngine.Video;
    14. [RequireComponent(typeof(VideoPlayer))]
    15.  
    16. /// <summary>
    17. ///     A custom handler that implements the ITrackableEventHandler interface.
    18. /// </summary>
    19. public class DefaultTrackableEventHandler : MonoBehaviour, ITrackableEventHandler
    20. {
    21.     #region PRIVATE_MEMBER_VARIABLES
    22.  
    23.     protected TrackableBehaviour mTrackableBehaviour;
    24. // setup the videoPlayer object
    25. private VideoPlayer videoPlayer;
    26.  
    27.     #endregion // PRIVATE_MEMBER_VARIABLES
    28.  
    29.     #region UNTIY_MONOBEHAVIOUR_METHODS
    30.  
    31.     protected virtual void Start()
    32.     {
    33.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    34.         if (mTrackableBehaviour)
    35.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    36.  
    37.   // add the following 4 lines to get the reference to the video player component of the plane that the video is attached to
    38.   // IMPORTANT: set "Video_plane" to the name of the plane game object you attached your video to
    39.   GameObject video = GameObject.Find ("Video_plane");
    40.   videoPlayer = video.GetComponent<VideoPlayer>();
    41.   videoPlayer.Play();
    42.   videoPlayer.Pause();
    43.   // see the VideoPlayer Scripting API for more ideas on which functions you can use in your code
    44.   // for example changing the playback speed or jumping to a speicific point in time:
    45.   // https://docs.unity3d.com/ScriptReference/Video.VideoPlayer.html
    46.  
    47.     }
    48.  
    49.     #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    50.  
    51.     #region PUBLIC_METHODS
    52.  
    53.     /// <summary>
    54.     ///     Implementation of the ITrackableEventHandler function called when the
    55.     ///     tracking state changes.
    56.     /// </summary>
    57.     public void OnTrackableStateChanged(
    58.         TrackableBehaviour.Status previousStatus,
    59.         TrackableBehaviour.Status newStatus)
    60.     {
    61.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    62.             newStatus == TrackableBehaviour.Status.TRACKED ||
    63.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    64.         {
    65.    //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    66.    // Play the video:
    67.    Debug.Log("Play!");
    68.    OnTrackingFound();
    69.    videoPlayer.Play();
    70.   }
    71.   else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
    72.    newStatus == TrackableBehaviour.Status.NOT_FOUND)
    73.   {
    74.    //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    75.    // Pause the video, if using Stop() the video would always play back from the beginning again
    76.    Debug.Log("Stop!");
    77.    OnTrackingLost();
    78.    videoPlayer.Pause();
    79.         }
    80.         else
    81.         {
    82.             // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
    83.             // Vuforia is starting, but tracking has not been lost or found yet
    84.             // Call OnTrackingLost() to hide the augmentations
    85.             OnTrackingLost();
    86.         }
    87.     }
    88.  
    89.     #endregion // PUBLIC_METHODS
    90.  
    91.     #region PRIVATE_METHODS
    92.  
    93.     protected virtual void OnTrackingFound()
    94.     {
    95.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    96.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    97.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    98.  
    99.         // Enable rendering:
    100.         foreach (var component in rendererComponents)
    101.             component.enabled = true;
    102.  
    103.         // Enable colliders:
    104.         foreach (var component in colliderComponents)
    105.             component.enabled = true;
    106.  
    107.         // Enable canvas':
    108.         foreach (var component in canvasComponents)
    109.             component.enabled = true;
    110.     }
    111.  
    112.  
    113.     protected virtual void OnTrackingLost()
    114.     {
    115.         var rendererComponents = GetComponentsInChildren<Renderer>(true);
    116.         var colliderComponents = GetComponentsInChildren<Collider>(true);
    117.         var canvasComponents = GetComponentsInChildren<Canvas>(true);
    118.  
    119.         // Disable rendering:
    120.         foreach (var component in rendererComponents)
    121.             component.enabled = false;
    122.  
    123.         // Disable colliders:
    124.         foreach (var component in colliderComponents)
    125.             component.enabled = false;
    126.  
    127.         // Disable canvas':
    128.         foreach (var component in canvasComponents)
    129.             component.enabled = false;
    130.     }
    131.  
    132.     #endregion // PRIVATE_METHODS
     
  2. ingebeekmans

    ingebeekmans

    Joined:
    Sep 12, 2018
    Posts:
    3
    I've got the exact same issue. So if anybody knows the answer to this, please share.
     
  3. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Unfortunately, the changes to the script generate errors in my development setting.

    Thats not specific enough. What are the errors, what are you expecting to happen that isn't happening or vice versa.
     
  4. ingebeekmans

    ingebeekmans

    Joined:
    Sep 12, 2018
    Posts:
    3
    As far as the script is concerned, the only error it gave me was "Assets/VideoPlay.cs(72,49): error CS0117: 'TrackableBehaviour.Status' does not contain a definition for 'NOT_FOUND'". I've fixed the error, but unfortunately the script is not solving the original problem. When the app is started, the video's (before they are detected) start playing. The audio, however, only starts playing when a target is detected. Therefore, the audio and video don't align.

    (I've created a short video in which I demonstrate the issue, you can find it here: https://drive.google.com/open?id=1spnjqS9q2Yc075Om_wlIqUM8lARfF6Jn . sorry for the crappy quality, my computer won't record the screen.)

    My question would be: Is there a function to stop (not pause) the video when target is lost? I'm using this piece of code for the audio:

    Code (CSharp):
    1.      void StopAllAudio()
    2.         {
    3.             allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
    4.             foreach (AudioSource audioS in allAudioSources)
    5.             {
    6.                 audioS.Stop();
    7.             }
    8.         }
     
  5. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Why would you want to stop / start the audiosource separately? The videoplayer should do so automatically, assuming you're playing the audio embedded in the video file and not a separate audio file.

    Could you post your entire trackable eventhandler script? I downloaded the adapted script from the youtube tutorial and it doesn't mention audiosources.
     
  6. ingebeekmans

    ingebeekmans

    Joined:
    Sep 12, 2018
    Posts:
    3
    I'm playing the audio as a separate audio file, because when I had the audio embedded in the video file, it did not mute/pause/stop the audio when the target was lost. This resulted in the audio looping indefinitely without a target being present (I made a video in which I demonstrate this issue: https://drive.google.com/open?id=1ed1EEjAQxOeJodiMcfE78Lznq0ygDwXz)

    Here's my current code:

    Code (CSharp):
    1. /*==============================================================================
    2. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    3. All Rights Reserved.
    4. Confidential and Proprietary - Protected under copyright and other laws.
    5. ==============================================================================*/
    6.  
    7. using UnityEngine;
    8.  
    9. //Add This script
    10. using System.Collections;
    11. using System.Collections.Generic;
    12.  
    13.  
    14. namespace Vuforia
    15. {
    16.     /// <summary>
    17.     /// A custom handler that implements the ITrackableEventHandler interface.
    18.     /// </summary>
    19.     public class DefaultTrackableEventHandler : MonoBehaviour,
    20.                                                 ITrackableEventHandler
    21.     {
    22.  
    23.         //------------Begin Sound----------
    24.         public AudioSource soundTarget;
    25.         public AudioClip clipTarget;
    26.         private AudioSource[] allAudioSources;
    27.  
    28.         //function to stop all sounds
    29.         void StopAllAudio()
    30.         {
    31.             allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
    32.             foreach (AudioSource audioS in allAudioSources)
    33.             {
    34.                 audioS.Stop();
    35.             }
    36.         }
    37.  
    38.         //function to play sound
    39.         void playSound(string ss)
    40.         {
    41.             clipTarget = (AudioClip)Resources.Load(ss);
    42.             soundTarget.clip = clipTarget;
    43.             soundTarget.loop = false;
    44.             soundTarget.playOnAwake = false;
    45.             soundTarget.Play();
    46.         }
    47.  
    48.         //-----------End Sound------------
    49.  
    50.  
    51.         #region PRIVATE_MEMBER_VARIABLES
    52.  
    53.         private TrackableBehaviour mTrackableBehaviour;
    54.  
    55.         #endregion // PRIVATE_MEMBER_VARIABLES
    56.  
    57.  
    58.  
    59.         #region UNTIY_MONOBEHAVIOUR_METHODS
    60.  
    61.         void Start()
    62.         {
    63.             mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    64.             if (mTrackableBehaviour)
    65.             {
    66.                 mTrackableBehaviour.RegisterTrackableEventHandler(this);
    67.             }
    68.  
    69.             //Register / add the AudioSource as object
    70.             soundTarget = (AudioSource)gameObject.AddComponent<AudioSource>();
    71.         }
    72.  
    73.         #endregion // UNTIY_MONOBEHAVIOUR_METHODS
    74.  
    75.  
    76.  
    77.         #region PUBLIC_METHODS
    78.  
    79.         /// <summary>
    80.         /// Implementation of the ITrackableEventHandler function called when the
    81.         /// tracking state changes.
    82.         /// </summary>
    83.         public void OnTrackableStateChanged(
    84.                                         TrackableBehaviour.Status previousStatus,
    85.                                         TrackableBehaviour.Status newStatus)
    86.         {
    87.             if (newStatus == TrackableBehaviour.Status.DETECTED ||
    88.                 newStatus == TrackableBehaviour.Status.TRACKED ||
    89.                 newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    90.             {
    91.                 OnTrackingFound();
    92.             }
    93.             else
    94.             {
    95.                 OnTrackingLost();
    96.             }
    97.         }
    98.  
    99.         #endregion // PUBLIC_METHODS
    100.  
    101.  
    102.  
    103.         #region PRIVATE_METHODS
    104.  
    105.  
    106.         private void OnTrackingFound()
    107.         {
    108.             Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    109.             Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    110.  
    111.             // Enable rendering:
    112.             foreach (Renderer component in rendererComponents)
    113.             {
    114.                 component.enabled = true;
    115.             }
    116.  
    117.             // Enable colliders:
    118.             foreach (Collider component in colliderComponents)
    119.             {
    120.                 component.enabled = true;
    121.             }
    122.  
    123.             Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    124.  
    125.  
    126.             //Play Sound, IF detect an target
    127.  
    128.             if (mTrackableBehaviour.TrackableName == "AR_Jan-1")
    129.             {
    130.                 playSound("audio/AR_Jan 1_audio");
    131.             }
    132.  
    133.             if (mTrackableBehaviour.TrackableName == "AR_Odile-1")
    134.             {
    135.                 playSound("audio/AR_Odile 1_audio");
    136.             }
    137.  
    138.         }
    139.  
    140.  
    141.         private void OnTrackingLost()
    142.         {
    143.             Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
    144.             Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
    145.  
    146.             // Disable rendering:
    147.             foreach (Renderer component in rendererComponents)
    148.             {
    149.                 component.enabled = false;
    150.             }
    151.  
    152.             // Disable colliders:
    153.             foreach (Collider component in colliderComponents)
    154.             {
    155.                 component.enabled = false;
    156.             }
    157.  
    158.             Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    159.  
    160.             //Stop All Sounds if Target Lost
    161.             StopAllAudio();
    162.         }
    163.  
    164.  
    165.  
    166.         #endregion // PRIVATE_METHODS
    167.     }
    168. }
    169.  
    170.  
    I'm happy with the way it's handling the audio at this moment. If the video-part could do the exact same thing as the audio-part, I'd be very happy. Hope you're able to help.
     
  7. moneshb7

    moneshb7

    Joined:
    Jan 27, 2019
    Posts:
    1
    Can you tell me how you solved this? "Assets/VideoPlay.cs(72,49): error CS0117: 'TrackableBehaviour.Status' does not contain a definition for 'NOT_FOUND'"
     
  8. cretinous

    cretinous

    Joined:
    Feb 24, 2019
    Posts:
    4
    I'm having this same issue and can't find any current code that can fix the issue. Any luck yet?
     
    Last edited: Feb 24, 2019
  9. thirdpurple

    thirdpurple

    Joined:
    Mar 21, 2019
    Posts:
    4
    I am having similar issues.
    An Ar camera
    ImageTarget with QUAD as child with the video player component.

    I have tried with embedded audio, a separate audio source
    Play on Awake On and off.

    But its never correct.
    The audio either starts immediately before the tracked image is found, and just carries on. The video does not pause when it stops tracking.
    Any help would b appreciated.
    jm
     
    cretinous likes this.
  10. hardikunity

    hardikunity

    Joined:
    Jun 19, 2019
    Posts:
    1
    instead of NOT FOUND write: Limited
     
  11. eminem1542

    eminem1542

    Joined:
    May 30, 2015
    Posts:
    1
    TrackableBehaviour.Status.NOT_FOUND)

    it was changed in unity 2019 by

    TrackableBehaviour.Status.NO_POSE)
     
    khanhabib likes this.
  12. santhoshkumarb7

    santhoshkumarb7

    Joined:
    Dec 22, 2019
    Posts:
    1
  13. kdsilva94

    kdsilva94

    Joined:
    Nov 11, 2021
    Posts:
    1
    So I'm using Unity 2020.3 I had the same issue, I was able to fix it by going to the "image target" -> "Video Trackable Event Handler" and changing the "Status Filter" from "Tracked_Extended Tracked" to "Tracked"

    Also thank you for the link to the Lecture, I needed to use the scripts he provided to get it to work