c # – Unity Tcp / IP Microphone Audio Allows Video Streaming

I am new to the unit.

I'm trying to stream audio from the microphone with live video from one app to another. Currently I have 2 apps. App 1 is the server / sender and app 2 is the client / receiver. In-App 1 I send the video bytes successfully to the client. and on the client side I also receive all bytes. I use sockets and TCP.

Now the problem is that I do not know how to send the microphone of the microphone together with the video from the server to the client.

The following code works perfectly for live video streaming from the server side

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;
with system;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;

public class Connecting: MonoBehaviour
{
WebCamTexture webCam;
public RawImage myImage;
public bool enableLog = false;

Texture2D currentTexture;

private TcpListener Listner;
private const int port = 8010;
private bool stop = false;

private list Customer = new list();

// This must be the same as SEND_COUNT on the client
const int SEND_RECEIVE_COUNT = 15;

Privatvoid Start ()
{
Application.runInBackground = true;

// start WebCam-Coroutine
StartCoroutine (initAndWaitForWebCamTexture ());
}


// Converts the data size into a byte array and places the result in the FullBytes array
void byteLengthToFrameByteArray (int byteLength, byte)[] full bytes)
{
// delete old data
Array.Clear (fullBytes, 0, fullBytes.Length);
// convert int to bytes
byte[] bytesToSendCount = BitConverter.GetBytes (byteLength);
// Copy result to fullBytes
bytesToSendCount.CopyTo (fullBytes, 0);
}

// Converts the byte array to the data size and returns the result
int frameByteArrayToByteLength (Byte)[] frameBytesLength)
{
int byteLength = BitConverter.ToInt32 (frameBytesLength, 0);
return byteLength;
}

IEnumerator initAndWaitForWebCamTexture ()
{
// Open the camera on the desired device, in my case IPAD Pro
webCam = new WebCamTexture ();
// Get all the equipment, front and rear camera
webCam.deviceName = WebCamTexture.devices[WebCamTexture.devices.Length - 1].Surname;

// Request the lowest width and height
webCam.requestedHeight = 10;
webCam.requestedWidth = 10;

myImage.texture = webCam;

webCam.Play ();

currentTexture = new Texture2D (webCam.width, webCam.height);

// connect to the server
Listner = new TcpListener (IPAddress.Any, Port);

listner.Start ();

while (webCam.width < 100)
        {
            yield return null;
        }

        //Start sending coroutine
        StartCoroutine(senderCOR());
    }

    WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();
    IEnumerator senderCOR()
    {

        bool isConnected = false;
        TcpClient client = null;
        NetworkStream stream = null;

        // Wait for client to connect in another Thread 
        Loom.RunAsync(() =>
        {
while (! stop)
{
// Wait for the client connection
client = listner.AcceptTcpClient ();
// We are connected
clients.Add (client);

isConnected = true;
stream = client.GetStream ();
}
});

// Wait for the client to connect
while (! isConnected)
{
Return yield zero;
}

LOG ("Connected!");

bool readyToGetFrame = true;

byte[] frameBytesLength = new byte[SEND_RECEIVE_COUNT];

while (! stop)
{
// wait for the end of the frame
Yield Return EndOfFrame;

currentTexture.SetPixels (webCam.GetPixels ());
byte[] pngBytes = currentTexture.EncodeToPNG ();
// Fill the total length of the byte to be sent. The result is stored in frameBytesLength
byteLengthToFrameByteArray (pngBytes.Length, frameBytesLength);

// set readyToGetFrame to false
readyToGetFrame = false;

Loom.RunAsync (() =>
{
// Send the total number of bytes first
stream.Write (frameBytesLength, 0, frameBytesLength.Length);
LOG ("byte by byte of the transmitted picture") + frameBytesLength.Length);

// Send the picture bytes
stream.Write (pngBytes, 0, pngBytes.Length);
LOG ("data field data byte send:" + pngBytes.Length);

//Cleverly. Set readyToGetFrame to true
readyToGetFrame = true;
});

// Wait until we're ready to get a new frame
while (! readyToGetFrame)
{
LOG ("wait for new frame");
Return yield zero;
}
}
}


void LOG (string message)
{
if (enableLog)
Debug.Log (message);
}

private void update ()
{
myImage.texture = webCam;
}

// stop everything
private void OnApplicationQuit ()
{
if (webCam! = null && webCam.isPlaying)
{
webCam.Stop ();
stop = true;
}

if (listner! = null)
{
listner.Stop ();
}

foreach (TcpClient c in customers)
c.Close ();
}
}

The following code works perfectly for live video streaming from the client side

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Net.Sockets;
using System.Net;
using System.IO;
with system;

Public Class Receiver: MonoBehaviour
{
public RawImage image;
public bool enableLog = false;

const intport = 8010;
public string IP = "192.168.1.165";
TcpClient client;

Texture2D Tex;

private bool stop = false;

// This must be the same as SEND_COUNT on the server
const int SEND_RECEIVE_COUNT = 15;

// Use this for initialization
void startup ()
{
Application.runInBackground = true;

tex = new Texture2D (0, 0);
Client = new TcpClient ();

// connect to the server from another thread
Loom.RunAsync (() =>
{
LOGWARNING ("Connection to the server ...");
// if on the desktop
client.Connect (IPAddress.Loopback, Port);

// if you use the iPad
//client.Connect(IPAddress.Parse(IP), port);
LOGWARNING ("Connected!");

imageReceiver ();
});
}


void imageReceiver ()
{
// While the loop is fine in another thread, we do not block a main-unity thread
Loom.RunAsync (() =>
{
while (! stop)
{
// read picture number
int imageSize = readImageByteSize (SEND_RECEIVE_COUNT);
LOGWARNING ("Received Image byte Length:" + imageSize);

// Read and display picture bytes
readFrameByteArray (imageSize);
}
});
}


// Converts the data size into a byte array and places the result in the FullBytes array
void byteLengthToFrameByteArray (int byteLength, byte)[] full bytes)
{
// delete old data
Array.Clear (fullBytes, 0, fullBytes.Length);
// convert int to bytes
byte[] bytesToSendCount = BitConverter.GetBytes (byteLength);
// Copy result to fullBytes
bytesToSendCount.CopyTo (fullBytes, 0);
}

// Converts the byte array to the data size and returns the result
int frameByteArrayToByteLength (Byte)[] frameBytesLength)
{
int byteLength = BitConverter.ToInt32 (frameBytesLength, 0);
return byteLength;
}


////////////// Image size from the server read //////////////////////////////////// /////////
private int readImageByteSize (int size)
{
bool disconnected = false;

NetworkStream serverStream = client.GetStream ();
byte[] imageBytesCount = new byte[size];
var total = 0;
do
{
var read = serverStream.Read (imageBytesCount, total, size - total);
//Debug.LogFormat("Client received {0} bytes, total);
if (read == 0)
{
disconnected = true;
break;
}
total + = read;
} while (total! = size);

int byteLength;

if (separated)
{
byteLength = -1;
}
otherwise
{
byteLength = frameByteArrayToByteLength (imageBytesCount);
}
return byteLength;
}

//////, --/////////////////////////////////////////////////////// Read array from server ////////////////////////////////////////////////////////////////////////////////////////////// //
private void readFrameByteArray (int size)
{
bool disconnected = false;

NetworkStream serverStream = client.GetStream ();
byte[] imageBytes = new byte[size];
var total = 0;
do
{
var read = serverStream.Read (imageBytes, total, size - total);
//Debug.LogFormat("Client received {0} bytes, total);
if (read == 0)
{
disconnected = true;
break;
}
total + = read;
} while (total! = size);

bool readyToReadAgain = false;

//View image
if (! separated)
{
// Show picture on the main thread
Loom.QueueOnMainThread (() =>
{
displayReceivedImage (imageBytes);
readyToReadAgain = true;
});
}

// Wait until the old picture is displayed
while (! readyToReadAgain)
{
System.Threading.Thread.Sleep (1);
}
}


void displayReceivedImage (Byte[] receiveImageBytes)
{
tex.LoadImage (receiveImageBytes);
image.texture = tex;
}


// update is called once per frame
void update ()
{


}


void LOG (string message)
{
if (enableLog)
Debug.Log (message);
}

void LOGWARNING (string notification)
{
if (enableLog)
Debug.LogWarning (Messaging);
}

void OnApplicationQuit ()
{
LOGWARNING ("OnApplicationQuit");
stop = true;

if (client! = null)
{
client.Close ();
}
}
}

My performance for video has been inferior so far, in the lower part audio is successfully played by the microphone

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Public class Audio1: MonoBehaviour
{
const int FREQUENCY = 44100;
Audio clip microphone;
int lastPos, pos;

// Use this for initialization
void startup ()
{
mic = Microphone.Start (null, true, 10, FREQUENCY);

AudioSource audio = GetComponent();
audio.clip = AudioClip.Create ("test", 10 * FREQUENCY, mic.channels, FREQUENCY, false);
audio.loop = true;

}

// update is called once per frame
void update ()
{
if ((pos = Microphone.GetPosition (null))> 0)
{
if (lastPos> pos) lastPos = 0;

if (pos - lastPos> 0)
{
// Assign the space for the sample.
hover[] sample = new float[(pos - lastPos) * mic.channels];

// get the data from the microphone.
mic.GetData (sample, lastPos);

// Paste the data into the audio source.
AudioSource audio = GetComponent();
audio.clip.SetData (sample, lastPos);

if (! audio.isPlaying) audio.Play ();

lastPos = pos;
}
}
}

void OnDestroy ()
{
Microphone.End (zero);
}
}

iphone – Audio becomes static when an app with a microphone is opened while a podcast is playing

I recently got an iPhone X and use the apps Overcast, Spotify and Discord.
When I use earphones (IconX, not Airpods) or Bluetooth audio in my car while listening to a podcast while using the Discord app, the audio changes drastically and sounds like a conference call. I've theoretically said that this has to do with the microphone, as going through these settings in discord will temporarily fix this. If you listen to music (instead of podcasts), it becomes completely silent instead.

I have no idea why this is, but I do not want this behavior to continue. I tried to enable Push to Talk on Discord, but it still happens. Does anyone know what is going on and how to disable this behavior?

Audio – How can I prevent my Mac from using my Mac automatically? [input only] USB microphone as the output device?

I have a MacBook Pro 2015 for the latest version of High Sierra. I often use a USB microphone for Skype, etc. When I plug in the microphone, it automatically recognizes my Mac as an input source. It's good. Anyway, it Likewise automatically detects it as the output source, which is not good at all, since the microphone has no output functions. Every time I plug in the microphone, I have to go to the sound settings and deselect the output.

Picture of the sound menu
How can I prevent my Mac from automatically recognizing my microphone as the source – and still recognize it as an input source?

Driver – noise very loud in the microphone

I'm using kubuntu 18.10, but this problem occurs in every distribution.
It is a microphone very loud. It is not the typical background noise, it is so, but louder and I can not hear my voice.

In Windows I do not have this problem. Well, I have another with the volume, and if I uninstall the audio driver, I get the same problem with the microphone.

What can I do?

My laptop is an Asus Q502l.
Card: HDA Intel PCH chip: Realtek ALC3229

Hide / disable microphone option on the Android soft keyboard

I want to hide the microphone in front of the soft keyboard in my current application.

I've tried these possible solutions for the same thing.

Disable the speech-text (micro-phone) key on the keyboard with the soft entry in Android programmatically

However, this solution does not work at all.

I found an unexpected thing when addType = "textFilter" is added, then it works for me, I mean, the microphone is hidden.

But if possible, I would like to have the exact solution to the problem, because if I take over the mentioned "textFilter" thing, it can break down later.

I tried it on below. versions

  1. Pie (API-28), device – Pixel2XL
  2. Marshmallow (API-23), Device – MotoE3Power