Here is part of the main code (networking was done separately):
void Update()
{
if (Network.connections.Length > 0)
{
audioFloat = new float[audioInfo.clip.samples * audioInfo.clip.channels];
audioInfo.clip.GetData (audioFloat, 0);
networkView.RPC("PlayMicrophone", RPCMode.Others, ToByteArray(audioFloat), audioInfo.clip.channels);
}
}
[RPC]
void PlayMicrophone(byte[] ba, int c)
{
float[] flar = ToFloatArray (ba);
audio.clip = AudioClip.Create ("test", flar.Length, c, 44100, true, false);
audio.clip.SetData (flar, 0);
audio.loop = true;
while (!(Microphone.GetPosition("") > 0)) {}
audio.Play();
}
void OnConnectedToServer()
{
audioInfo.clip = Microphone.Start(null, true, 100, 44100);
}
void OnPlayerConnected(NetworkPlayer player)
{
audioInfo.clip = Microphone.Start(null, true, 100, 44100);
}
public byte[] ToByteArray(float[] floatArray) {
int len = floatArray.Length * 4;
byte[] byteArray = new byte[len];
int pos = 0;
foreach (float f in floatArray) {
byte[] data = System.BitConverter.GetBytes(f);
System.Array.Copy(data, 0, byteArray, pos, 4);
pos += 4;
}
return byteArray;
}
public float[] ToFloatArray(byte[] byteArray) {
int len = byteArray.Length / 4;
float[] floatArray = new float[len];
for (int i = 0; i < byteArray.Length; i+=4) {
floatArray[i/4] = System.BitConverter.ToSingle(byteArray, i);
}
return floatArray;
}
I know that it probably will not play perfectly, but I expected to hear even the slightest sound even for a second, but this never happened. It seems that the data is being transmitted via RPC, but the sound is not playing.
In the Update () function, it constantly calls RPC to start playback from the microphone. This happens after the connection has been made as a client or server.
Since you cannot send any arrays other than byte arrays, I get the audio data as a floating-point array, and then convert it to a byte array that will be sent to another connected person. There are no errors when starting the code, but the sound still does not play.
, , . , 100 ; - .
, , , . .
.
, !