Log in or register to post comments

Instantiating Video as prefab will not autoplay

April 8, 2014 - 8:38am #1

I am trying to have a video be created with the press of a button and I want it to start playing automatically. I turned the video into a prefab and I am loading it using

newVideo = GameObject.Instantiate(myVideoas GameObject;

the video gets loaded into the scene but will not play automatically even though I have the videoPlaybackBehaviour on it and the flag set to true for autoplay. I am not sure what I am missing if anyone could give me some advice. I have a second image in the scene with a video on it that will autoplay but that one is always in the scene.

 

thank you

Instantiating Video as prefab will not autoplay

April 11, 2014 - 2:52am #10

Thanks for the update.

Glad you were able to get this working :)

N

Instantiating Video as prefab will not autoplay

April 10, 2014 - 7:29am #9

I have multiple videos that I wanted to show on the same image in the same place, I ended up putting them all in there and then setting them to inactive and activating them when I needed them to show up and that worked.

Instantiating Video as prefab will not autoplay

April 10, 2014 - 2:49am #8

The other image does not have anything attached to it but when it is scanned by AR camera on my android a GUI button shows up which lets my instantiate the video prefab

 

I do not understand why you are trying to instantiate a video prefab at run-time?  Have you checked and compared it against the one at design time?

 

N

 

Instantiating Video as prefab will not autoplay

April 9, 2014 - 10:44am #7

m_autoplay stays true but the new video does not seem to be responding to it normally

Instantiating Video as prefab will not autoplay

April 9, 2014 - 10:22am #6

What did you learn by stepping through it with the debugger?

N

Instantiating Video as prefab will not autoplay

April 9, 2014 - 7:31am #5

I am familiar with recreating the scene and have done that a few times already. Here is what is happening I have two images in my scene one with prefab video on it already wich works exactly as it is supposed to. The other image does not have anything attached to it but when it is scanned by AR camera on my android a GUI button shows up which lets my instantiate the video prefab. The prefab shows up and will start playing if I click on it but it will not autoplay. I have tried setting the m_autoplay bool to always be true in the script but that did not help.

Instantiating Video as prefab will not autoplay

April 9, 2014 - 1:48am #4

This exercise may help you.

The objective here is to show how to replicate the essence of the Vuforia-VideoPlayback sample scene using the Vuforia prefabs and the drag and drop approach of Unity

·      Create a new Unity project

·      Import the Vuforia video playback unity package

·      Create a new scene

·      Drag the ARCamera prefab into the Unity scene

o   Under the DataSetLoadBehaviour in the Inspector tick “Load Data Set StonesAndChips”, and the Activate checkbox below this

·      From Qualcomm AugmentedReality/Prefabs drag the ImageTarget prefab into the scene

o   For the Image Target select “StonesAndChips” as the dataset and the Image Target should change to the Stones texture

·      From Vuforia Video Playback/Prefabs drag the Video prefab to be the child of the Image Target

o   In the Inspector under “Video Playback Behaviour (Script)” set the path to VuforiaSizzleReel_1.m4v

·      Drag the TrackableEventHandler from Scripts to the Image Target (this plays the video)

·      Remove the DefaultTrackableEventHandler script from the Image Target as it is not needed.

·      Autoplay works already, however tapping the video does not yet work  In order to fix this simply create a VideoPlaybackController script, and fill with the code below), and then attach this to the ARCamera.

 

HTH

N

 

VideoPlaybackController.cs script – to enable tap to play the video 

using UnityEngine;
using System.Collections;

/// <summary>
/// This class contains the logic to handle taps on VideoPlaybackBehaviour game objects
/// and starts playing the according video. It also pauses other videos when a new one is
/// started.
/// </summary>
public class VideoPlaybackController : MonoBehaviour
{
    #region PRIVATE_MEMBER_VARIABLES

    private Vector2 mTouchStartPos;
    private bool mTouchMoved = false;
    private float mTimeElapsed = 0.0f;

    private bool mTapped = false;
    private float mTimeElapsedSinceTap = 0.0f;

    private bool mWentToFullScreen = false;

    #endregion // PRIVATE_MEMBER_VARIABLES

    #region UNITY_MONOBEHAVIOUR_METHODS

    void Update()
    {
        // Determine the number of taps
        // Note: Input.tapCount doesn't work on Android

        if (Input.touchCount > 0)
        {
            Touch touch = Input.touches[0];
            if (touch.phase == TouchPhase.Began)
            {
                mTouchStartPos = touch.position;
                mTouchMoved = false;
                mTimeElapsed = 0.0f;
            }
            else
            {
                mTimeElapsed += Time.deltaTime;
            }

            if (touch.phase == TouchPhase.Moved)
            {
                if (Vector2.Distance(mTouchStartPos, touch.position) > 40)
                {
                    // Touch moved too far
                    mTouchMoved = true;
                }
            }
            else if (touch.phase == TouchPhase.Ended)
            {
                if (!mTouchMoved && mTimeElapsed < 1.0)
                {
                    if (mTapped)
                    {
                        // Second tap
                        HandleDoubleTap();
                        mTapped = false;
                    }
                    else
                    {
                        // Wait to see if this is a double tap
                        mTapped = true;
                        mTimeElapsedSinceTap = 0.0f;
                    }
                }
            }
        }

        if (mTapped)
        {
            if (mTimeElapsedSinceTap >= 0.5f)
            {
                // Not a double tap
                HandleTap();
                mTapped = false;
            }
            else
            {
                mTimeElapsedSinceTap += Time.deltaTime;
            }
        }

        // special handling in play mode:
        if (QCARRuntimeUtilities.IsPlayMode())
        {
            if (Input.GetMouseButtonUp(0))
            {
                if (PickVideo(Input.mousePosition) != null)
                    Debug.LogWarning("Playing videos is currently not supported in Play Mode.");
            }
        }
    }

    #endregion // UNITY_MONOBEHAVIOUR_METHODS

    #region PRIVATE_METHODS

    /// <summary>
    /// Handle single tap event
    /// </summary>
    private void HandleTap()
    {
        // Find out which video was tapped, if any
        VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);

        if (video != null)
        {
            if (video.VideoPlayer.IsPlayableOnTexture())
            {
                // This video is playable on a texture, toggle playing/paused

                VideoPlayerHelper.MediaState state = video.VideoPlayer.GetStatus();
                if (state == VideoPlayerHelper.MediaState.PAUSED ||
                    state == VideoPlayerHelper.MediaState.READY ||
                    state == VideoPlayerHelper.MediaState.STOPPED)
                {
                    // Pause other videos before playing this one
                    PauseOtherVideos(video);

                    // Play this video on texture where it left off
                    video.VideoPlayer.Play(false, video.VideoPlayer.GetCurrentPosition());
                }
                else if (state == VideoPlayerHelper.MediaState.REACHED_END)
                {
                    // Pause other videos before playing this one
                    PauseOtherVideos(video);

                    // Play this video from the beginning
                    video.VideoPlayer.Play(false, 0);
                }
                else if (state == VideoPlayerHelper.MediaState.PLAYING)
                {
                    // Video is already playing, pause it
                    video.VideoPlayer.Pause();
                }
            }
            else
            {
                // Display the busy icon
                video.ShowBusyIcon();
                
                // This video cannot be played on a texture, play it full screen
                video.VideoPlayer.Play(true, 0);
                mWentToFullScreen = true;
            }
        }
    }

    /// <summary>
    /// Handle double tap event
    /// </summary>
    private void HandleDoubleTap()
    {
        // Find out which video was tapped, if any
        VideoPlaybackBehaviour video = PickVideo(mTouchStartPos);

        if (video != null)
        {
            if (video.VideoPlayer.IsPlayableFullscreen())
            {
                // Pause the video if it is currently playing
                video.VideoPlayer.Pause();

                // Seek the video to the beginning();
                video.VideoPlayer.SeekTo(0.0f);

                // Display the busy icon
                video.ShowBusyIcon();

                // Play the video full screen
                video.VideoPlayer.Play(true, 0);
                mWentToFullScreen = true;
            }
        }
    }

    /// <summary>
    /// Find the video object under the screen point
    /// </summary>
    private VideoPlaybackBehaviour PickVideo(Vector3 screenPoint)
    {
        VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
                FindObjectsOfType(typeof(VideoPlaybackBehaviour));

        Ray ray = Camera.main.ScreenPointToRay(screenPoint);
        RaycastHit hit = new RaycastHit();

        foreach (VideoPlaybackBehaviour video in videos)
        {
            if (video.collider.Raycast(ray, out hit, 10000))
            {
                return video;
            }
        }

        return null;
    }

    /// <summary>
    /// Pause all videos except this one
    /// </summary>
    private void PauseOtherVideos(VideoPlaybackBehaviour currentVideo)
    {
        VideoPlaybackBehaviour[] videos = (VideoPlaybackBehaviour[])
                FindObjectsOfType(typeof(VideoPlaybackBehaviour));

        foreach (VideoPlaybackBehaviour video in videos)
        {
            if (video != currentVideo)
            {
                if (video.CurrentState == VideoPlayerHelper.MediaState.PLAYING)
                {
                    video.VideoPlayer.Pause();
                }
            }
        }
    }

    #endregion // PRIVATE_METHODS

    #region PUBLIC_METHODS

    /// <summary>
    /// One-time check for the Instructional Screen
    /// </summary>
    public bool CheckWentToFullScreen()
    {
        bool result = mWentToFullScreen;
        mWentToFullScreen = false;
        return result;
    }

    #endregion // PUBLIC_METHODS
}

 

Instantiating Video as prefab will not autoplay

April 8, 2014 - 10:38am #3

I tried creating a new project and only loading one of the videos that come in the videoplayback sample as a prefab and that still did not work. Do you know if creating as a prefab is changing a setting somewhere?

Instantiating Video as prefab will not autoplay

April 8, 2014 - 9:24am #2

I suggest opening up a second instance of Unity and trying to identify what the difference is with your scene/project.

This might explain the different behaviour.

 

N

Log in or register to post comments