1
\$\begingroup\$

I have a UI with a button and a panel. Clicking the button toggles the panel on and off :

GameObject.SetActive();

Now the panel element has a child which is a plane. This plane has the following components :

  • mesh renderer
  • videoPlay, a script I've written
  • an audio source

The videoPlay script basically applies the MovieTexture to the plane's material, sets the plane's audioSource to the movie's audio clip, and starts audio and video.

public MovieTexture movie;

void Start() {
    GetComponent<Renderer>().material.mainTexture = movie as MovieTexture;
    GetComponent<AudioSource>().clip = movie.audioClip;
    movie.Play();
    GetComponent<AudioSource>().Play();
}

Now when at runtime I click the button (which hides the panel which is the plane's parent element), the audio and video stop as the plane disappears - which is expected behaviour.

When I reopen the panel though, the sound restarts right away but as if it were catching up for the time it was hidden (so it starts really fast and then comes back to regular speed) ; and the video is just frozen for - empirically and approximately - the same time the panel was hidden.

I've tried in the script - in Update() - messing with

if (!transform.parent.gameObject.activeSelf && movie.isPlaying)
    {
        movie.Pause();
        GetComponent<AudioSource>().Pause();
    }

for example, (and the same with Play() when it shows back up) but it doesn't seem to change anything.

\$\endgroup\$
  • \$\begingroup\$ You need to pause before you de-activate the gameobject, not after. \$\endgroup\$ – jgallant Apr 5 '17 at 17:18
  • \$\begingroup\$ Makes sense. I'll add a script to the button to make the toggle (right now I'm using unity itself), and play/pause appropriately. \$\endgroup\$ – Saryk Apr 5 '17 at 17:22
  • \$\begingroup\$ If that solves the issue, make sure to add it as an answer and (after the required delay) mark it accepted - that will make it easier for other users with similar problems to find the solution. \$\endgroup\$ – DMGregory Apr 5 '17 at 19:40
1
\$\begingroup\$

As @jgallant stated, it was a matter of pausing the A/V before deactivating the panel.

I rearranged the scripts and works fine, still with a small delay though.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.