Question by
dahid123 · May 09, 2016 at 01:04 AM ·
unity 5videosynchronization
Audio is out of sync with my video in Unity
Hi i am having problems with videos in Unity. I am using a Raw Image to display and play my video, however Unity is not playing the first few seconds of my video and therefore my audio is out of sync. Does anyone have a workaround for this?
My code is below if this helps:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent (typeof(AudioSource))]
public class PlayVideo : MonoBehaviour {
public MovieTexture movie2;
private AudioSource movie2audio;
void Start () {
GetComponent<RawImage>().texture = movie2 as MovieTexture;
movie2audio = GetComponent<AudioSource>();
movie2audio.clip = movie2.audioClip;
movie2.Play();
movie2audio.Play();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && movie2.isPlaying)
{
movie2.Pause();
}
else if (Input.GetKeyDown(KeyCode.Space) && !movie2.isPlaying)
{
movie2.Play();
}
}
}
Comment
Answer by JKrulz · May 10, 2016 at 05:59 AM
While I'm not sure of what the actual cause is, what you might be able to do is using the WaitForSeconds function so that way it lines up perfectly with your video. For example, if your audio is 3 seconds behind, you could use WaitForSeconds(3) for calling the video 3 seconds later.
Cheers for your help, i ended up fixing the problem by rendering my video file to .mov and it worked fine!