Question by
zakpacha · Mar 31, 2019 at 03:35 PM ·
androidaudiosourceandroidplugin
[Android] AudioSource stops playing music when recording with MediaRecorder
I have been trying to figure this little bug out for a while now and it's driving me crazy.
.
Whenever I begin recording the screen on an android device using the MediaRecorder, the audiosources in my scene will most of the time stop playing. I am also using OnAudioFilterRead to capture audio but I think I have ruled that out as the problem.
// write the incoming audio to the output string
void OnAudioFilterRead(float[] data, int channels)
{
if (this.Rendering)
{
short[] intData = new short[data.Length]; //initialize intData to be of equal length to dataSource
byte[] byteData = new byte[data.Length * 2]; //initialize byteData to be 2x length of dataSource (2 bytes per short)
for (int i = 0; i < data.Length; i++)
{
float temp = data[i] * RESCALE_FACTOR; //scale float value to prevent data loss when casting to short
intData[i] = (short)temp; //fill intData with scaled float temp after short cast
byte[] byteArr = new byte[2]; //initialize a temporary array byteArr to store single short value
byteArr = BitConverter.GetBytes(intData[i]); //store short value in byteArr
byteArr.CopyTo(byteData, i * 2); //copy byteArr data to byteData at proper index
}
// store the data stream
this.outputWriter.Write(byteData);
}
}
Android plugin code:
//used by Unity side to start recording
public void startRecording() {
try {
initRecorder();
shareScreen();
} catch (IOException e) {
e.printStackTrace();
}
UnityPlayer.UnitySendMessage(this.mGameObject, this.mMethodName, "OnRecordStart");
}
//init the ProjectionManager to create a virtual Display and start recording screen
private void shareScreen() {
if (this.mMediaProjection == null) {
startActivityForResult(this.mProjectionManager.createScreenCaptureIntent(), 200);
return;
}
this.mVirtualDisplay = createVirtualDisplay();
this.mRecorder.start();
}
private VirtualDisplay createVirtualDisplay() {
return this.mMediaProjection.createVirtualDisplay("MainActivity", this.mDisplayMetrics.widthPixels, this.mDisplayMetrics.heightPixels, this.mDisplayMetrics.densityDpi, 16, this.mRecorder
.getSurface(), null, null);
}
//prepare to record audio from mic and video from screen
private void initRecorder() throws IOException {
this.mRecorder = new MediaRecorder();
//this.mRecorder.setAudioSource(1);
this.mRecorder.setVideoSource(2);
this.mRecorder.setOutputFormat(2);
this.mRecorder.setOutputFile(this.mFilePath);
this.mRecorder.setVideoSize(this.mDisplayMetrics.widthPixels, this.mDisplayMetrics.heightPixels);
this.mRecorder.setVideoEncoder(2);
//this.mRecorder.setAudioEncoder(1);
this.mRecorder.setVideoEncodingBitRate(this.mBitRate);
this.mRecorder.setVideoFrameRate(this.mFps);
this.mRecorder.prepare();
}
Comment
Your answer
Follow this Question
Related Questions
CommandInvokationFailure: Unable to merge android manifests. See the Console for more details. 1 Answer
How can I open a fragment view from inside Unity Android? 0 Answers
How to override string resources of other android plugins in Unity 0 Answers
How to get the low level rendering plugin working on Android? 1 Answer