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.
- 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.
- 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.
- 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.
- 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)