Question by
retxab · Dec 02, 2017 at 06:46 AM ·
videoaudiosourcescriptingproblem
Updating AudioSource settings while game is running through script.
I have a Game Object with a script that is playing a video. This is happening through a video player and outputting the audio to another Game Object's Audio Source. I am trying to program a button that will toggle muting the audio from the video. This works fine if I just output the audio through the same Game Object that is playing the video. However, when I output the Audio Source to a separate game object the Audio never changes. The Audio Source shows its mute value changing, but this has no effect on the audio in the game.
Here is the code for the Game Object that plays the video.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
public class videoScript: MonoBehaviour {
public GameObject plane;
public VideoClip videoToPlay;
public AudioSource audioSource;
private VideoPlayer videoPlayer;
private VideoSource videoSource;
private bool isMuted = false;
void Start() {
//adds videoplayer to the gameObject
videoPlayer = gameObject.AddComponent<VideoPlayer>();
//adds audioSource to the gameObject
//audioSource = gameObject.AddComponent<AudioSource>();
//Starts the audio, and video, but mutes the audio.
videoPlayer.playOnAwake = true;
audioSource.playOnAwake = true;
audioSource.mute = isMuted;
//Sets audio output to go to an Audio Source
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
//Assign the Audio from Video to AudioSource to be played
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
//sets the video to loop when the end is reached
videoPlayer.isLooping = true;
//Set video To Play then prepare Audio to prevent Buffering
videoPlayer.clip = videoToPlay;
videoPlayer.Prepare();
//Wait until video is prepared
float startTime = Time.deltaTime;
while (!videoPlayer.isPrepared) {
startTime = startTime + Time.deltaTime;
Debug.Log("Preparing Video");
//Break out of the while loop after 5 seconds wait
if (startTime >= 5) {
break;
}
}
Debug.Log("Done Preparing Video");
//Assign the Texture from Video to the plane to be displayed
plane.GetComponent<Renderer>().material.SetTexture("_MainTex", videoPlayer.texture);
//Play Video
videoPlayer.Play();
//Play Sound
audioSource.Play();
Debug.Log("Playing Video");
/*while (videoPlayer.isPlaying) {
Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
}*/
Debug.Log("Done Playing Video");
}
void Update() {
audioSource.clip = videoPlayer.GetTargetAudioSource(0).clip;
audioSource.mute = isMuted;
}
public void muteVideo() {
isMuted = !isMuted;
}
}
Here is the code for the mute button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class muteButtonScript : MonoBehaviour {
public GameObject videoObject;
private void OnMouseDown() {
Debug.Log("The mute button has been pressed");
}
private void muteVideo() {
videoObject.GetComponent<TvScript>().muteVideo();
}
}
Comment