Live Ingame sound doesn't work with headphones
I'm trying to have live in-game sound. Somebody is doing stuff in VR, and another person can make a character talk in game. It has to be live. It works so far but when I plug in headphones the microphone and sound don't work anymore. Without headphones, the microphone works and in-game sound too but it's very bad quality and a lot of static. How do I fix the in-game sound, and how do I make headphones work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class LipSync : MonoBehaviour
{
public AudioSource audiosource;
public AudioClip audioClip;
public bool useMirco;
public string selectedDevice;
public AudioMixerGroup mixerGroupMic, mixerGroupMas;
float[] samples;
public int parts = 1024;
void Start()
{
samples = new float[parts];
//micro input
if (useMirco)
{
if(Microphone.devices.Length > 0)
{
selectedDevice = Microphone.devices[0].ToString();
audiosource.outputAudioMixerGroup = mixerGroupMic;
audiosource.clip = Microphone.Start(selectedDevice, true, 3400, AudioSettings.outputSampleRate);
}
else
{
useMirco = false;
}
}
if (!useMirco)
{
audiosource.outputAudioMixerGroup = mixerGroupMas;
audiosource.clip = audioClip;
}
audiosource.Play();
}
float Volume()
{
float volume = 0;
if (audiosource.isPlaying)
{
for (int can = 0; can < 2; can++)
{
audiosource.GetOutputData(samples, can);
for (int i = 0; i < parts; i++)
{
volume += Mathf.Abs(samples[i]);
}
}
volume = volume / parts;
}
return volume;
}
void Update()
{
transform.position = transform.parent.position - transform.up * Volume();
}
}
Comment