Issues when a movie is played in a coroutine

I’ve encountered some strange issues when playing a movie in a coroutine. I’m able to reproduce them consistently using the attached unitypackage and the steps below. I’ve reproduced them on two machines so far, both running Unity Pro (version 3.3.0f4) on Windows Vista.

I tried reporting this as a bug through Unity twice, but I can’t see any of the info that I wrote in the bug report. Since the first one was closed with a canned reply, I’m wondering if the reports I wrote weren’t submitted correctly. Or, possibly, these issues weren’t reproducible on QA’s machines.

Anyway, here’s my report on the issues (with a few small additions). I’m curious to see if others are affected by this as well, because it seems like it would arise fairly often.

  1. What happened

If a movie is played in a coroutine, then two issues occur.

Issue 1: If the movie is stopped and played again, e.g. by returning to the Videos menu and then playing the same video a second time, then the last-visible frame and sound will be played just before the movie restarts.

Issue 2: After a couple of scene changes, e.g. returning to the Videos Menu and then the Main Menu, the menu background music pauses unexpectedly. The music resumes if the game loses and regains focus.

  1. How can we reproduce it using the example you attached

Do the following to reproduce issue 1:

a. Create a new project and import the attached package.
b. In the Build Settings, add the three scenes.
c. Open the Main Menu scene and run it – OR – build and run the game with the Main Menu as scene 0.
— You’ll hear the menu music start.
d. Click on the text in the middle to navigate to the Videos Menu.
e. Click on the text in the lower-middle to play the video.
— You’ll hear the menu music stop.
— You’ll see the movie’s audio and video start.
f. Let the movie play for a few seconds, then click on the text in the bottom-right to return to the Videos Menu.
— You’ll see the movie’s audio and video stop.
— You’ll hear the menu music start again.
g. Play the video again.
— You’ll hear the menu music stop again.
— BUG: for 1 frame, you’ll see the movie’s audio and video resume from where you stopped them. After 1 frame, the movie will start from the beginning.

Continue to reproduce issue 2:

h. Return to the Videos Menu.
— You’ll see the movie’s audio and video stop.
— You’ll hear the menu music start again.
i. Navigate back to the Main Menu.
— BUG: you’ll hear the menu music pause unexpectedly. You can resume the menu music by taking focus away from the game and returning to it.

  1. Workaround

Both MovieTexture.Play() and AudioSource.Play() must be called in a non-coroutine method. For instance, in the attached example, you can toggle the bug by clicking on the text at the bottom of the menu screens. When the bug is “disabled”, the movie’s audio and video are played in the Start method instead of a coroutine, and the issues above do not occur.

EDIT: Thanks to dreamora, there is a better solution: simply set AudioSource.clip to null when the PlayVideo script is destroyed.

  1. Other information

It is possible for these issues to occur even if MovieTexture.Play() and AudioSource.Play() are called in a non-coroutine method. But it is not consistently reproducible.

In our actual game, we encountered these issues with the following setup:

  • Update method calls PlayVideo.
  • PlayVideo method calls MovieTexture.Play() and AudioSource.Play().
  • PlayVideo method starts a coroutine.

I attempted to add this case to our example project and managed to reproduce it at first. (I called PlayVideo from Start rather than from Update, but otherwise it was the same.) I was modifying the example UI to allow you to choose this case, but after changing the UI and adding some switch statements to choose different cases, the bug stopped occurring.

In the end, I decided to submit the example project without this extra case. Hopefully, by fixing the consistent bug described at the top, the inconsistent bug that affects our game will also be fixed.

628414–22412–$Play Video In Coroutine Bugs.unitypackage (2.83 MB)

the issues are pretty surely reproducable as the movie replay startup mess is known and pretty tricky to workaround (I just recently had the joy of doing it again) as the time the “mess frame” is there is not constant nor a given number of frames.

I didn’t have any problems with the sound though only with the “echo from the past” frame.

As for the music pause: sure you take the movie audio clip, assign it to an audio source and play and remove later on again? if you don’t and don’t take control of it you risk that you play too many sounds in parallel then others will be stopped and prevented from playing as there are only a limited number of audio channels that can play in parallel

Thanks for the reply, dreamora! This is very interesting. I tried your suggestion and set the AudioSource’s clip to null when the video player is destroyed. This one change fixed BOTH issues in my post: the video always restarted immediately from the beginning, and the music no longer paused when I returned to the Main Menu. In fact, I’m going to edit it into the Workarounds section.

So for now, the issues don’t seem to be a problem: set the audio clip to null and everything works great (in that example project, at least). Still, I found some interesting new information about this bug:

  1. Play the game in the editor.
  2. “Disable” the bug – OR – add dreamora’s fix to the PlayVideo script by setting audio.clip to null in OnDestroy.
  3. Navigate to the Video Player scene.
  4. In the Video Player scene, select the Cube game object (the one with the movie AudioSource, Renderer, and PlayVideo script attached).
  5. Navigate back to the Videos Menu.

You can now reproduce both issues again. Playing the video again causes the movie-starts-where-it-left-off problem; Returning to the Main Menu causes the music-pauses problem. This initially freaked me out as I was working on our game and the bug suddenly occurred again even though I was sure I had worked around it. I was relieved to discover it only happened because of something I’d selected in the Editor.

Another detail: that Cube game object (I wish I had named it better) has to be selected in the Inspector at the time that you leave the Video Player scene in order to reproduce the issues in this way. If you select it and then select something else, everything works fine.

Obviously, since players can’t “select” the video player when the game is actually being played, this isn’t really a problem, but hopefully this extra information will make the source of the bug easier to track down and solve in the future.