Log in or register to post comments

Make a video loop

September 29, 2014 - 1:29pm #1

Hello all,

 

I tried the videoplayback sample and it works fine. The video plays when you click on it. However, I want the video to play automatically when it is detected but when it finishes, I want it to loop back to the first frame. Also when it is lost from the camera's view and viewed again, I want it to start over from the first frame and not resume from where it stopped. 

How can I achieve this?

Make a video loop

August 23, 2017 - 3:14am #9

When i paste the code in Unity 5.6 i get a compile error and it won´t work. can anybody point me in the right direction ? greatly appreciated...Thanks !

 

Make a video loop

March 21, 2017 - 5:03am #8

Thank you !!! Works fine

Make a video loop

November 14, 2016 - 12:47pm #7

It works a bit differently in the newest Vuforia v6.

But thanks a lot for pointing me into the right direction :) I've partially used the provided code, and it works like a charm. I appreciate your help.

Make a video loop

March 16, 2016 - 8:22am #6

Great answer. Life saver. Thank you

Make a video loop

July 17, 2015 - 6:19am #5

Thank you so much for your help and your effort, Aestial. It works nicely!

Make a video loop

July 16, 2015 - 12:00pm #4

mGrohmann wrote:

Hello!

I'm also trying to make a video loop. Auto Play works fine but since I'm new to C# I don't really know how to make it loop and where to change the scripts. Is there any way you could give a more detailed answer?

Thanks a lot in advance!

Hello Grohmann,

I realized how to archieve the video loop. You just have to make a few changes in the VideoPlaybackBehaviour.cs script (not included in Vuforia basic assets).

First you have to add the following condition to the HandleStateChange function, just after checking the VideoPlayerHelper's state:

else if (newState == VideoPlayerHelper.MediaState.REACHED_END) 
{
	mVideoPlayer.Play(false, 0);
}

Then, you have to comment out the switch sentence that displays the (Play/Pause/Error) Icon, because it flashes for a moment just before the video loops.

// Display the appropriate icon, or disable if not needed
        switch (newState)
        {
            case VideoPlayerHelper.MediaState.READY:
            case VideoPlayerHelper.MediaState.REACHED_END:
            case VideoPlayerHelper.MediaState.PAUSED:
            case VideoPlayerHelper.MediaState.STOPPED:
                mIconPlane.renderer.material.mainTexture = m_playTexture;
                mIconPlaneActive = true;
                break;

            case VideoPlayerHelper.MediaState.NOT_READY:
            case VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN:
                mIconPlane.renderer.material.mainTexture = m_busyTexture;
                mIconPlaneActive = true;
                break;

            case VideoPlayerHelper.MediaState.ERROR:
                mIconPlane.renderer.material.mainTexture = m_errorTexture;
                mIconPlaneActive = true;
                break;

            default:
                mIconPlaneActive = false;
                break;
        }

This is how the whole HandleStateChange function should look:

    // Handle video playback state changes
    private void HandleStateChange(VideoPlayerHelper.MediaState newState)
    {
        // If the movie is playing or paused render the video texture
        // Otherwise render the keyframe
        if (newState == VideoPlayerHelper.MediaState.PLAYING ||
			newState == VideoPlayerHelper.MediaState.PAUSED) {
			renderer.material.mainTexture = mVideoTexture;
			renderer.material.mainTextureScale = new Vector2 (1, 1);
		} 
		else if (newState == VideoPlayerHelper.MediaState.REACHED_END) 
		{
			mVideoPlayer.Play(false, 0);
		} 
		else
        {
            if (mKeyframeTexture != null)
            {
                renderer.material.mainTexture = mKeyframeTexture;
                renderer.material.mainTextureScale = new Vector2(1, -1);
            }
        }
		/*
        // Display the appropriate icon, or disable if not needed
        switch (newState)
        {
            case VideoPlayerHelper.MediaState.READY:
            case VideoPlayerHelper.MediaState.REACHED_END:
            case VideoPlayerHelper.MediaState.PAUSED:
            case VideoPlayerHelper.MediaState.STOPPED:
                mIconPlane.renderer.material.mainTexture = m_playTexture;
                mIconPlaneActive = true;
                break;

            case VideoPlayerHelper.MediaState.NOT_READY:
            case VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN:
                mIconPlane.renderer.material.mainTexture = m_busyTexture;
                mIconPlaneActive = true;
                break;

            case VideoPlayerHelper.MediaState.ERROR:
                mIconPlane.renderer.material.mainTexture = m_errorTexture;
                mIconPlaneActive = true;
                break;

            default:
                mIconPlaneActive = false;
                break;
        }
		*/
        if (newState == VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN)
        {
            // Switching to full screen, disable QCARBehaviour (only applicable for iOS)
            QCARBehaviour qcarBehaviour = (QCARBehaviour) FindObjectOfType(typeof(QCARBehaviour));
            qcarBehaviour.enabled = false;
        }
        else if (mCurrentState == VideoPlayerHelper.MediaState.PLAYING_FULLSCREEN)
        {
            // Switching away from full screen, enable QCARBehaviour (only applicable for iOS)
            QCARBehaviour qcarBehaviour = (QCARBehaviour) FindObjectOfType(typeof(QCARBehaviour));
            qcarBehaviour.enabled = true;
        }
    }

Also, you can find my modified script attached (Change .txt to .cs). Hope this help you!

 

AttachmentSize
Plain text icon VideoPlaybackBehaviour.txt13.32 KB

Make a video loop

July 9, 2015 - 7:49am #3

Hello!

I'm also trying to make a video loop. Auto Play works fine but since I'm new to C# I don't really know how to make it loop and where to change the scripts. Is there any way you could give a more detailed answer?

Thanks a lot in advance!

Make a video loop

October 2, 2014 - 5:12pm #2

In Unity, you can activate the AutoPlay checkbox in the Inspector settings for the video object.

For looping you can use the methods in the VideoPlayerHelper.cs such as GetLength() to find out when the video reaches the end and set its seek position back to 0.

For starting video over you can set the seek position back to 0 when the video is paused/stopped. Look at the TrackableEventHandler.cs for the code that is pausing the video when tracking is lost.

Log in or register to post comments