Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by timehunter · Mar 02, 2021 at 12:59 PM · playervideolooping

How to loop a video seamless

As read in many forums, using built-in looping in VideoPlayer doesn't work well as there is a short audio break before the video restarts. I've tried many solutions... but no way. In the end I've tried to adapt a solution found in StackOverflow according to my needs:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.Video;

 public class MegaPlayer : MonoBehaviour
 {
     private VideoPlayer[] players = new VideoPlayer[2];
     public RenderTexture targetTexture;
     public string url;

     void Awake()
     {
         for (int i = 0; i < players.Length; i++) {
             players[i] = gameObject.AddComponent<VideoPlayer>();
             players[i].source = VideoSource.Url;
             players[i].url = url;
             players[i].playOnAwake = false;
             players[i].waitForFirstFrame = true;
             players[i].isLooping = false;
             players[i].skipOnDrop = true;
             players[i].targetTexture = targetTexture;
         }
     }

     void Start()
     {
         StartCoroutine(Play(0));
     }

     IEnumerator Play(int activePlayer)
     {
         // wait until the video is ready
         players[activePlayer].Prepare();
         while (!players[activePlayer].isPrepared) {
             yield return null;
         }

         players[activePlayer].Play();

         // wait until the current video has not finished
         bool reachedHalfWay = false;
         int nextPlayer = activePlayer ^ 1;
         while (players[activePlayer].isPlaying) {
             if (!reachedHalfWay && players[activePlayer].time >= (players[activePlayer].length / 2)) {;
                 reachedHalfWay = true;
                 players[nextPlayer].time = 0f;
                 players[nextPlayer].Prepare();
             }
             yield return null;
         }

         while (!players[nextPlayer].isPrepared) {
             yield return null;
         }

         StartCoroutine(Play(nextPlayer));
     }
 }

The idea is to simulate isLooping by creating 2 VideoPlayer instances that alternate each other. When one play is half the way, I start preparing the second one so that it is ready before the current play ends.

The problem is that the result is a pure mess. It looks like both players start together anyway. Does anybody have a better idea on how to implement a seamless video looping?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by pauldarius98 · Mar 02, 2021 at 02:05 PM

What i think that is happening is that when you Add the VideoPlayer, it will automatically play (because by default playOnAwake is on) and Awake is called when the object is created, before you assign the value of playOnAwake to off. What you can do is simply add at line 21 the following code:

 players[i].Stop();

Either the solution above or add the videoPlayers manually on the gameObject and turn off playOnAwake (then do the setup in awake as it is now but you will already have a refference for the array of VideoPlayer so you no longer need GetComponent).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image timehunter · Mar 02, 2021 at 04:03 PM 0
Share

Thank u very much for the hint... but am I not creating the players manually and assigning them to the current gameObject at line 14? Perhaps I'm missing something...

avatar image pauldarius98 timehunter · Mar 02, 2021 at 04:08 PM 0
Share

I was talking about assign them in the editor. When AddComponent is called the Awake method from VideoPlayer runs and so when you do players[i].playOnAwake = false is too late cause the VideoPlayer already initialized and started to play the video (not completely sure but most likely this is the case). just call the Stop method as i described in the answer and it should work

avatar image timehunter pauldarius98 · Mar 02, 2021 at 06:14 PM 0
Share

Thank u very much for your support. It was really helpfull :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Videoplayer basic controls guide.. 0 Answers

Answer For -> How to Reward Player after watching video - admob rewarded video 0 Answers

pre-prepare video in scene before its needed 0 Answers

How to Loop just the end (last quarter-ish) portion of a 360 video? 0 Answers

iOS Video Player-Unity Pro features in the Basic 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges