Reputation: 27
I am trying to record a video and save it to a file but I keep getting an error when it comes to the recording. The code is
using System;
using Android.App;
using Android.OS;
using Android.Widget;
using Android.Media;
using System.Timers;
using Android.Content;
using System.IO;
namespace ShowHouseDemo2._1
{
[Activity(Label = "VideoPage", Icon = "@drawable/icon")]
//Intent intent = GetInTent();
class VideoPage : Activity
{
//Intent string Vid = Intent.GetStringExtra("VidSent");
MediaRecorder recorder;
Timer StartTimer;
int StartCount = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.VidTakerPage);
var video = FindViewById<VideoView>(Resource.Id.UserVid);
//string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath. "/UserVid4.mp4";
// string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + ;
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine(documents, "Write.txt");
File.WriteAllText(filename, "Write this text into a file!");
video.StopPlayback();
recorder = new MediaRecorder();
recorder.SetVideoSource(VideoSource.Camera);
recorder.SetAudioSource(AudioSource.Mic);
recorder.SetOutputFormat(OutputFormat.Default);
recorder.SetVideoEncoder(VideoEncoder.Default);
recorder.SetAudioEncoder(AudioEncoder.Default);
recorder.SetOutputFile(filename);
recorder.SetPreviewDisplay(video.Holder.Surface);
recorder.Prepare();
recorder.Start();
timerStarted();
}
private void timerStarted()
{
base.OnResume();
StartTimer = new Timer();
StartTimer.Interval = 1000;
StartTimer.Elapsed += delegate
{
while (StartCount < 10)
{
if (StartCount == 9)
{
RunOnUiThread(() =>
{
Intent k = new Intent(this, typeof(TakeVideo));
this.StartActivity(k);
});
}
StartCount++;
}
};
}
}
}
the Error that I get is :
Unhandled Exception:
Java.IO.IOException: invalid preview surface.
I love any bit of help you can give me. Any questions that you have please ask
Upvotes: 0
Views: 1350
Reputation: 74209
You need to implement the surface callback interface (Android.Views.ISurfaceHolderCallback
) and only start using the ISurfaceHolder.Holder
when it is available and valid.
You can do this on your VideoPage Activity, i.e.
[Activity(Label = "VideoPage", Icon = "@drawable/icon")]
public class VideoPage : Activity, Android.Views.ISurfaceHolderCallback
Implement the three methods of the interface (SurfaceDestroyed
, SurfaceCreated
, SurfaceChanged
) and then you can assign the callback via the VideoView.Holder.AddCallback
:
var video = FindViewById<VideoView>(Resource.Id.UserVid);
video.Holder.AddCallback((this));
Upvotes: 2