I am using a SurfaceView
which shows the stream of the drone on my smartphone and what I want is to record the stream of the drone, but it's not that easy. So I think I could use MediaRecorder
for this. For this problem I have read many posts, but they didn't help me out.
In the OnCreate
I am creating the SurfaceView
:
mPreview = FindViewById<SurfaceView>(Resource.Id.surfaceView1);
holder = mPreview.Holder;
holder.AddCallback(this);
holder.SetFormat(Format.Rgba8888);
In the SurfaceCreated
override I added the SetPreviewDisplay
:
mRecorder = new Android.Media.MediaRecorder();
mRecorder.SetPreviewDisplay(mPreview.Holder.Surface);
So, when I click on the record button I start the following method in a new thread, otherwise the SurfaceView
will be stuck:
private void StartRecord()
{
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string path = System.IO.Path.Combine(sdCardPath, "test.mp4");
file = new File(path);
mRecorder.SetVideoSource(Android.Media.VideoSource.Surface);
mRecorder.SetOutputFormat(Android.Media.OutputFormat.Mpeg4);
mRecorder.SetVideoEncoder(Android.Media.VideoEncoder.H264);
mRecorder.SetVideoEncodingBitRate(512 * 1000);
mRecorder.SetVideoFrameRate(30);
mRecorder.SetOutputFile(file.AbsolutePath);
mRecorder.SetVideoSize(mPreview.Width, mPreview.Height);
mRecorder.Prepare();
mRecorder.Start();
}
There are no errors until the mRecorder.Stop()
will be called:
private void StopRecord()
{
if(mRecorder != null)
{
try
{
mRecorder.Stop();
}
catch (Java.Lang.RuntimeException e)
{
// Here he always comes
file.Delete();
System.Console.WriteLine(e);
}
finally
{
mRecorder.Release();
mRecorder = null;
}
}
}
When I check into the mRecorder
variable, it's saying at the surface object: failed to get surface
. That's strange because when I check the mRecorder
variable after the mRecorder
started, it just has a surface object.
What am I doing wrong?