12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Video;
- namespace Imagine.WebAR.Samples
- {
- public class SyncVideoSound : MonoBehaviour
- {
- [SerializeField] VideoPlayer video;
- [SerializeField] AudioSource sound;
- public float lastPos = 0;
- void Awake(){
-
- }
- void OnEnable(){
- StartCoroutine("SyncRoutine");
- }
- void OnDisable(){
- StopCoroutine("SyncRoutine");
- }
-
- IEnumerator SyncRoutine()
- {
- var videoRenderer = video.GetComponent<Renderer>();
- videoRenderer.enabled = false;
- while(!video.isPrepared){
- Debug.Log("Waiting video preparation");
- yield return null;
- }
- video.Play();
- sound.Play();
- video.time = lastPos;
- //sound.time = lastPos;
- while(true){
- if(video.time > 0.01f)
- {
- videoRenderer.enabled = true;
- }
- else if(!sound.isPlaying){
- sound.time = (float)video.time;
- sound.Play();
- }
-
- if(Mathf.Abs(sound.time - (float)video.time) > 0.1){
- Debug.Log(sound.time + ", " + sound.clip.length);
-
- sound.time = (float)video.time;
- Debug.Log(sound.time + "=>" + video.time);
- }
-
- lastPos = (float)video.time;
- //yield return new WaitForSeconds(0.05f);
- yield return null;
- }
- }
-
- }
- }
|