Hello Android developers!
I am trying to do data link between the Android and Unity with sound buffer. I have gone through tutorials to make my own test plugin. I can now record audio file to disc on the device and then play it back with the device using Unity for UI yet handling everything else on Android side.
What I now try to do is actually capture a sound sample from Android, pass it as Short to Unity.
So what I do is setup the AudioRecord.read and have it on Runnable to continuously capture and then I would need way to read the buffer from Unity. This is the problem point, I am not confident enough to build the mechanics and handle the buffer reading in proper way, from Unity, so I can not really make good decision how the Android side should be done to make it work as full. Here is my sample code:
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
public class NativeAudioCapture
{
short buffer;
short[] soundDataBuffer;
int bufferSize;
int BUF_SIZE;
String LOG_TAG;
Thread myThread;
int SAMPLE_RATE = 16000;
boolean recording=true;
AudioRecord myAudioRecord;
public void do_loopback()
{
myThread = new Thread
(
new Runnable()
{
public void run()
{
loopback();
}
}
);
}
public void loopback()
{
// Prepare the AudioRecord & AudioTrack
try
{
bufferSize = AudioRecord.getMinBufferSize
(
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT
);
if (bufferSize <= BUF_SIZE)
{
bufferSize = BUF_SIZE;
}
Log.i(LOG_TAG,"Initializing Audio Record and Audio Playing objects");
myAudioRecord = new AudioRecord
(
MediaRecorder.AudioSource.MIC,
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize * 1
);
}
catch (Throwable t)
{
Log.e("Error", "Initializing Audio Record and Play objects Failed "+t.getLocalizedMessage());
}
myAudioRecord.startRecording();
Log.i(LOG_TAG,"Audio Recording started");
while (recording == true)
{
myAudioRecord.read(soundDataBuffer, 0, BUF_SIZE); // http://developer.android.com/intl/ja/reference/android/media/AudioRecord.html#read%28short[],%20int,%20int%29
// How to return soundDataBuffer to Unity?
}
Log.i(LOG_TAG, "loopback exit");
}
}
I am afraid that this approach with Runnable is somehow wrong way around. That maybe I should call a function from Unity to just get one sample with all the initialization, but then I assume it would be way too slow and better would be to just try read the buffer directly and let Android handle the capture totally.
As I am starting to learn this, I am utterly ignorant. Apologies for that. My main question is how to setup everything that it would allow me to read the buffer from Unity?
I would really appreciate a little nudge to right direction how to tackle this problem.