UnoDosCode UnoDosCode - 8 days ago 6
C# Question

AVCaptureFileOutputRecordingDelegate Compile Error when trying to implement Video recording in Xamarin Forms iOS with AVFoundation

I am trying to create a video recording with AVFoundation where I use this project as my standard project. https://github.com/ThatCSharpGuy/Forms-FullCameraPage

Now i am trying to build a start/stop recording function with AVFoundation but I am getting stuck because I am having trouble creating a new instance with

AVCaptureFileOutputRecordingDelegate
. Project is not able to run because i get the error message:
Cannot create an instance of the abstract class or interface "AVFoundation.AVCaptureFileOutputRecordingDelegate"
.

I found a post that goes through the same problem https://forums.xamarin.com/discussion/33925/avcapturefileoutputrecordingdelegate-compile-error but a solution was not found there.

Below you can see my code where I have commented on every step of the way.

Boolean weAreRecording;

public override async void ViewDidLoad()
{
base.ViewDidLoad();
SetupLiveCameraStream();
SetupEventHandlers();
weAreRecording = false;
}


Inside my
SetupLiveCameraStream
i additionally add (the project i linked has the rest of the code inside this function):

Console.WriteLine("Configuring output");
output = new AVCaptureMovieFileOutput();

long totalSeconds = 10000;
Int32 preferredTimeScale = 30;
CMTime maxDuration = new CMTime(totalSeconds, preferredTimeScale);
output.MinFreeDiskSpaceLimit = 1024 * 1024;
output.MaxRecordedDuration = maxDuration;

if (captureSession.CanAddOutput(output))
{
captureSession.AddOutput(output);
}

captureSession.SessionPreset = AVCaptureSession.PresetMedium;


... and also:

var mic = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
var micInput = AVCaptureDeviceInput.FromDevice(mic);
captureSession.AddInput(micInput);


Then I set up the event handlers.

private void SetupEventHandlers()
{
takeVideoButton.TouchUpInside += (s, e) => //created a button so when i click it my function startStopPushed will begin record
{
this.takeVideoButton.TouchUpInside += startStopPushed;

};
}


This is my start/stop recording function below where the compile error occurs. I am having trouble creating a
new AVCaptureFileOutputRecordingDelegate
as I mentioned above with the errormessage
Cannot create an instance of the abstract class or interface "AVFoundation.AVCaptureFileOutputRecordingDelegate"
, I have commented in the code where you can find this:

AVCaptureMovieFileOutput output;

private void startStopPushed (object sender, EventArgs ea)
{

if (!weAreRecording)
{
System.Diagnostics.Debug.WriteLine("start recording");
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var library = System.IO.Path.Combine(documents, "..", "Library");
var urlpath = System.IO.Path.Combine(library, "sweetMovieFilm.mov");

NSUrl url = new NSUrl(urlpath, false);

NSFileManager manager = new NSFileManager();
NSError error = new NSError();

if (manager.FileExists(urlpath))
{
Console.WriteLine("Deleting File");
manager.Remove(urlpath, out error);
Console.WriteLine("Deleted File");
}



AVCaptureFileOutputRecordingDelegate avDel= new AVCaptureFileOutputRecordingDelegate (); //Error message here: Cannot create an instance of the abstract class or interface `AVFoundation.AVCaptureFileOutputRecordingDelegate`
output.StartRecordingToOutputFile(url, avDel);
Console.WriteLine(urlpath);
weAreRecording = true;

}

//we were already recording. Stop recording
else {

output.StopRecording();

System.Diagnostics.Debug.WriteLine("stopped recording");

weAreRecording = false;


}
}


So my question is how can I solve this AVCaptureFileOutputRecordingDelegate problem?

Answer

thats because AVCaptureFileOutputRecordingDelegat has abstract function FinishedRecording. You cant create Instance of class having abstract. You need to create subclass of AVCaptureFileOutputRecordingDelegat and implement abstract methods before using it.

create subclass like this:

public class myAvCaptureFileOutPutRecordingDelegate : AVCaptureFileOutputRecordingDelegat
    {
        public override void FinishedRecording(AVCaptureFileOutput captureOutput, Foundation.NSUrl outputFileUrl, Foundation.NSObject[] connections, Foundation.NSError error)
        {
            //You can use captureOutput and outputFileUrl here.. 
            //throw new NotImplementedException();
        }


    }

now, create instance of this class:

 AVCaptureFileOutputRecordingDelegate avDel= new myAvCaptureFileOutPutRecordingDelegate();