Vuforia - 如何在识别图像时播放一个视频

时间:2021-06-20 21:30:00

I have an AR application that has 4 image targets, and each image target has a corresponding video that is played on top of it when it is recognized. The problem is that even though the correct animation is displayed whenever the image is recognized, all of the other animations are played as well, even though they are not displayed. Thus, whenever I move from one image target to another, the video is displayed, but it is already halfway through its runtime.

我有一个AR应用程序,它有4个图像目标,每个图像目标有一个相应的视频,当它被识别时在它上面播放。问题在于,即使在识别图像时显示正确的动画,也会播放所有其他动画,即使它们未被显示。因此,每当我从一个图像目标移动到另一个图像目标时,就会显示视频,但它已经在运行时间的一半。

I want each video to start whenever its parent image target is recognized, but only that specific video. I need control of the trackable behavior for each image target/video.

我希望每个视频在识别其父图像目标时启动,但仅限于该特定视频。我需要控制每个图像目标/视频的可跟踪行为。

how can i do that? I know that the scripts I need to look at in Unity are DefaultTrackableEventHandler.cs or mediaPlayerCtrl.cs (which is a scrip that I am using for my video manager(video player) that I got from the Easy Movie Texture asset. What is the code that I need to write in those scripts to make it happen. Here is the DefaultTrackableEventHandler.cs code:

我怎样才能做到这一点?我知道我需要在Unity中查看的脚本是DefaultTrackableEventHandler.cs或mediaPlayerCtrl.cs(这是我用于我从Easy Movie Texture资产获得的视频管理器(视频播放器)的脚本。什么是我需要在这些脚本中编写代码来实现它。这是DefaultTrackableEventHandler.cs代码:

using UnityEngine;
using Vuforia;

/// <summary>
///     A custom handler that implements the ITrackableEventHandler 
interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour, 
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES

protected TrackableBehaviour mTrackableBehaviour;

#endregion // PRIVATE_MEMBER_VARIABLES

#region UNTIY_MONOBEHAVIOUR_METHODS

protected virtual void Start()
{
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    if (mTrackableBehaviour)
        mTrackableBehaviour.RegisterTrackableEventHandler(this);
}

#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)
    {
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
        OnTrackingFound();
    }
    else if (previousStatus == TrackableBehaviour.Status.TRACKED &&
             newStatus == TrackableBehaviour.Status.NOT_FOUND)
    {
        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
        OnTrackingLost();
    }
    else
    {
        // For combo of previousStatus=UNKNOWN + newStatus=UNKNOWN|NOT_FOUND
        // Vuforia is starting, but tracking has not been lost or found yet
        // Call OnTrackingLost() to hide the augmentations
        OnTrackingLost();
    }
}

#endregion // PUBLIC_METHODS

#region PRIVATE_METHODS

protected virtual void OnTrackingFound()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);

    // Enable rendering:
    foreach (var component in rendererComponents)
        component.enabled = true;

    // Enable colliders:
    foreach (var component in colliderComponents)
        component.enabled = true;

    // Enable canvas':
    foreach (var component in canvasComponents)
        component.enabled = true;
}


protected virtual void OnTrackingLost()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);

    // Disable rendering:
    foreach (var component in rendererComponents)
        component.enabled = false;

    // Disable colliders:
    foreach (var component in colliderComponents)
        component.enabled = false;

    // Disable canvas':
    foreach (var component in canvasComponents)
        component.enabled = false;
}

#endregion // PRIVATE_METHODS
}

1 个解决方案

#1


0  

You have to stop the video everytime tracking is lost, and start it again when tracking is found. I don't know exactly how your video asset work, but I will use generic names/functions so you get the idea

每次跟踪丢失时都必须停止视频,并在找到跟踪时再次启动视频。我不确切知道您的视频资产是如何工作的,但我会使用通用名称/功能让您明白这一点

protected virtual void OnTrackingLost()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // Disable rendering:
    foreach (var component in rendererComponents)
        component.enabled = false;

    // Disable colliders:
    foreach (var component in colliderComponents)
        component.enabled = false;

    // Disable canvas':
    foreach (var component in canvasComponents)
        component.enabled = false;

    // Stop video
    myVideo.Stop();
}

Similarly for OnTrackingFound

同样适用于OnTrackingFound

protected virtual void OnTrackingFound()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // Enable rendering:
    foreach (var component in rendererComponents)
        component.enabled = true;

    // Enable colliders:
    foreach (var component in colliderComponents)
        component.enabled = true;

    // Enable canvas':
    foreach (var component in canvasComponents)
        component.enabled = true;

    // Play the video again from the beggining
    myVideo.Play();
}

#1


0  

You have to stop the video everytime tracking is lost, and start it again when tracking is found. I don't know exactly how your video asset work, but I will use generic names/functions so you get the idea

每次跟踪丢失时都必须停止视频,并在找到跟踪时再次启动视频。我不确切知道您的视频资产是如何工作的,但我会使用通用名称/功能让您明白这一点

protected virtual void OnTrackingLost()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // Disable rendering:
    foreach (var component in rendererComponents)
        component.enabled = false;

    // Disable colliders:
    foreach (var component in colliderComponents)
        component.enabled = false;

    // Disable canvas':
    foreach (var component in canvasComponents)
        component.enabled = false;

    // Stop video
    myVideo.Stop();
}

Similarly for OnTrackingFound

同样适用于OnTrackingFound

protected virtual void OnTrackingFound()
{
    var rendererComponents = GetComponentsInChildren<Renderer>(true);
    var colliderComponents = GetComponentsInChildren<Collider>(true);
    var canvasComponents = GetComponentsInChildren<Canvas>(true);
    var myVideo = GetComponentInChildren<TheVideoClassAttachedToChildOfImageTarget>

    // Enable rendering:
    foreach (var component in rendererComponents)
        component.enabled = true;

    // Enable colliders:
    foreach (var component in colliderComponents)
        component.enabled = true;

    // Enable canvas':
    foreach (var component in canvasComponents)
        component.enabled = true;

    // Play the video again from the beggining
    myVideo.Play();
}