Unity Video script not playing sound - unity3d

Im using a plugin from the unity asset store and trying to get video playing with sound - currently its not. Here is the script that plays the video --
using UnityEngine;
using UnityEngine.Video;
namespace Complete360Tour {
[AddComponentMenu("Complete360Tour/Media/VideoMediaReactor")] public class VideoMediaReactor : MonoBehaviour, IMediaSwitchReactor<VideoMediaNodeData> {
//-----------------------------------------------------------------------------------------
// Inspector Variables:
//-----------------------------------------------------------------------------------------
[Header("Assignment")] [SerializeField] protected MediaView mediaView;
[SerializeField] protected VideoPlayer videoPlayer;
//-----------------------------------------------------------------------------------------
// Private Fields:
//-----------------------------------------------------------------------------------------
private RenderTexture renderTexture;
//-----------------------------------------------------------------------------------------
// Unity Lifecycle:
//-----------------------------------------------------------------------------------------
protected void Awake() {
if (mediaView == null) Debug.LogWarning("No MediaView assigned. Please assign a MediaView.");
}
//-----------------------------------------------------------------------------------------
// Public Methods:
//-----------------------------------------------------------------------------------------
public void SwitchMedia(VideoMediaNodeData data, MediaSwitchStates state) {
if (data == null) {
InvalidSwitchData();
return;
}
switch (state) {
case MediaSwitchStates.BeforeSwitch: break;
case MediaSwitchStates.Switch:
BeginVideo(data.VideoClip);
mediaView.SetStereoscopic(data.IsStereo);
break;
case MediaSwitchStates.AfterSwitch: break;
}
}
public void ExitMedia() { InvalidSwitchData(); }
//-----------------------------------------------------------------------------------------
// Private Methods:
//-----------------------------------------------------------------------------------------
private void InvalidSwitchData() {
videoPlayer.Stop();
videoPlayer.targetTexture = null;
mediaView.SetMedia(null);
mediaView.SetStereoscopic(false);
}
private void BeginVideo(VideoClip videoClip) {
renderTexture = new RenderTexture((int) videoClip.width, (int) videoClip.height, 0);
videoPlayer.clip = videoClip;
videoPlayer.targetTexture = renderTexture;
mediaView.SetMedia(renderTexture);
videoPlayer.Play();
}
}
}
I tried adding the audio stuff following
http://justcode.me/unity2d/how-to-play-videos-on-unity-using-new-videoplayer/
but no luck. Audio still wouldn't play.

In case you are using the Unity Video Player:
Try adding Audio Source Component to the GameObject holding the video player
Drag and drop the Audio source to the Audio Source in Video Player component

Related

How do I change the pitch of audio when slowing down time?

I've been making a game using Unity and I added a slow motion function. Afterwards, when I added audio, I wanted to change the pitch of all audio whenever the slow motion function ocurred, but I can't seem to do it. I've been using Brackey's audio tutorial (here if you wanna see it) to guide me into using audio in Unity
Here is my audio manager:
using UnityEngine.Audio;
using System;
public class soundManager : MonoBehaviour
{
public Sound[] sounds;
void Awake()
{
foreach(Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
s.source.Play();
}
}
here is my slow motion script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
public float slowdownFactor = 0.05f;
public float slowdownLength = 2.0f;
private void Update()
{
Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
}
public void DoSlowMotion()
{
Time.timeScale = slowdownFactor;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
}
}
I wanted to change the pitch of all audio
If you want to change the pitch of any song at runtime you can simply use the source of type AudioSource that is saved in the sound class and edit it's values directly.
If you then do this as a foreach loop, in your soundManager class, with each song in your array, you can pitch down all of them.
Change All Pitch Values:
public void ChangeAllPitchValues(float pitchDifference) {
// Loop through our whole sound array.
foreach (Sound s in sounds) {
// Adjust pitch value equal to the given difference.
s.source.pitch += pitchDifference;
}
}
Additionally it seems like you are missing the singleton pattern that Brackeys used. You should probably add that as well to easily call the soundManager from any other script including your TimeManager.
#region Singelton
public static soundManager instance;
private void Awake() {
// Check if instance is already defined
// and if this gameObject is not the current instance.
if (instance != null) {
Debug.LogWarning("Multiple instances found. Current instance was destroyed.");
Destroy (gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
foreach (Sound s in sounds) {
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
#endregion
Once you've added the singleton pattern and the new function in the soundManager, then you can simply call that function from your DoSlowMotion() method.
Call the Method:
public void DoSlowMotion() {
...
// Pitch all songs down to 0.95f instead of 1f.
soundManager.instance.ChangeAllPitchValues(-slowdownFactor);
}

Volume Control for Audio source from VideoClp from VideoPlayer

I'm trying to setup a volume control slider for my video player but having a tough time trying to figure out how to control the volume in the video clip inside Unity Video Player. I can't seem to link the audio source to the video clip so I can control the volume. I don't have any problem with individual mp3 audio files but can't get it to work with video files.
Any suggestions?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using UnityEngine.EventSystems;
public class track : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public AudioSource audio;
public Slider audiovolume;
public VideoPlayer video;
Slider tracking;
bool slide = false;
void Start()
{
tracking = GetComponent<Slider>();
}
public void OnPointerDown(PointerEventData a)
{
slide = true;
}
public void OnPointerUp(PointerEventData a)
{
float frame = (float)tracking.value * (float)video.frameCount;
video.frame = (long)frame;
slide = false;
}
void Update()
{
if (!slide && video.isPlaying)
tracking.value = (float)video.frame / (float)video.frameCount;
}
public void volume()
{
audio.volume = audiovolume.value;
}
}
Your code does not seem to link te audiovolume slider in any way.
Try this:
void Start()
{
tracking = GetComponent<Slider>();
if (audiovolume!=null && audio!=null)
audiovolume.onValueChanged.AddListener((x)=>audio.volume=x);
}

Unity Game Object Controlled by real light spot on the wall

We have got various controllers developed for moving gameobjects. I have used magnetometer/gyro sensor to move game object using MUVSlide through following code:
using UnityEngine;
using ForestIndieGames.Muvslide;
public class Connection : MonoBehaviour {
private static bool created = false;
private bool newInputAvailable;
private MuvslideConnection muvslideConn;
private void Awake()
{
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
muvslideConn = new MuvslideConnection();
}
}
private void OnApplicationQuit()
{
if (muvslideConn != null)
muvslideConn.Close();
}
private void Update()
{
if (muvslideConn.GetInputManager().IsNewMotionAvailable())
newInputAvailable = true;
}
public bool IsNewInputAvailable()
{
bool result = newInputAvailable;
newInputAvailable = false;
return result;
}
public Vector3 GetAngles()
{
float[] angles = muvslideConn.GetInputManager().GetOrientationDegrees();
return new Vector3(angles[0], angles[1], angles[2]);
}
}
What I am trying to achieve is to move a gameobject by a real light spot on the wall. The spot is on the wall and fed through a camera. When the spot moves I want game object to follow exactly. The light spot can be a specific color or IR or UV etc.
Any leads please

Unity Vuforia can't make child as an image target to appear

I am trying make my prefabs appear at runtime on my imagetarget by following this link
vuforia instantiate prefab on imagetarget dynamically
the only difference is that I want to be able to drag some prefabs that I created instead of only one prefab shown in the tutorial.
public GameObject[] prefabModels;
my problem is even after my imagetarget is detected all my 3d prefabs didn't show up.
this code is attached to my imagetarget object
using System;
using UnityEngine;
using System.Collections;
using Vuforia;
public class ImageTargetMgr : MonoBehaviour, ITrackableEventHandler {
private TrackableBehaviour mTrackableBehaviour;
public GameObject[] prefabModels;
// Use this for initialization
void Start () {
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
// Update is called once per frame
void Update () {
}
public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
{
OnTrackingFound();
}
else
{
OnTrackingLost();
}
}
private void OnTrackingLost()
{
}
private void OnTrackingFound()
{
Debug.Log("Image Target Found!");
foreach (GameObject o in prefabModels)
{
if (o != null)
{
Debug.Log("Currently instantiated models is " + o.name);
GameObject myPrefab = Instantiate(o, Vector3.zero, Quaternion.identity) as GameObject;
myPrefab.transform.parent = mTrackableBehaviour.transform;
myPrefab.transform.localPosition = new Vector3(0.0f, 10.0f, 0.0f);
myPrefab.transform.localRotation = Quaternion.identity;
//o.transform.localScale = new Vector3(1000.0f, 1000.0f, 1000.0f);
myPrefab.gameObject.SetActive(true);
}
}
}
here are the log and what shown on hierarchy when running the scene
hope I will get some helps here..cheers
The problem finally solved after I added Box Collider component to the prefab object. I have to tweak also some of the transform settings.
Below are the screen captures of the prefab's inspector panel at runtime

Unity movie texture audio does not sync

When I try to play a movie on UNITY 5.1.2 RawImage.
It works, but the audio is way out of sync with the movie and I can't figure out why.
Here is the code that plays the movie:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//Does not support mobile
[RequireComponent(typeof(AudioSource))]
public class Movie : MonoBehaviour
{
public bool playOnStart = true;
#if !(UNITY_ANDROID || UNITY_IPHONE) || UNITY_EDITOR
public MovieTexture movieTexture;
#endif
public AudioSource audioSource;
IEnumerator Start ()
{
if (audioSource == null) {
audioSource = GetComponent<AudioSource> ();
}
audioSource.Stop ();
if (playOnStart) {
Play ();
}
yield return 0;
}
/// <summary>
/// Play the movie
/// </summary>
public void Play ()
{
#if !(UNITY_ANDROID || UNITY_IPHONE) || UNITY_EDITOR
if(movieTexture == null){
Debug.Log("Movie texture is undefined");
return;
}
GetComponent<RawImage>().enabled = true;
GetComponent<RawImage>().texture = movieTexture;
movieTexture.Play ();
audioSource.clip = movieTexture.audioClip;
audioSource.Play ();
#endif
}
}
It is attached to a RawImage object with a RawImage script attached to it. I want to play the movie when a player presses a button on a certain location.
Thanks