我正在尝试使用AVFoundation创建视频录制,我将此项目用作我的标准项目。 https://github.com/ThatCSharpGuy/Forms-FullCameraPage
现在我正在尝试使用AVFoundation构建一个启动/停止录制功能,但我遇到了困难,因为我在使用AVCaptureFileOutputRecordingDelegate
创建新实例时遇到了问题。项目无法运行,因为我收到错误消息:Cannot create an instance of the abstract class or interface "AVFoundation.AVCaptureFileOutputRecordingDelegate"
。
我发现了一个经历同样问题的帖子https://forums.xamarin.com/discussion/33925/avcapturefileoutputrecordingdelegate-compile-error但是找不到解决方案。
下面你可以看到我的代码,我已经评论了每一步。
Boolean weAreRecording;
public override async void ViewDidLoad()
{
base.ViewDidLoad();
SetupLiveCameraStream();
SetupEventHandlers();
weAreRecording = false;
}
在我的SetupLiveCameraStream
内部我另外添加(我链接的项目在此函数中包含其余代码):
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;
......还有:
var mic = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
var micInput = AVCaptureDeviceInput.FromDevice(mic);
captureSession.AddInput(micInput);
然后我设置了事件处理程序。
private void SetupEventHandlers()
{
takeVideoButton.TouchUpInside += startStopPushed;
}
这是我在下面发生编译错误的开始/停止录制功能。我在上面提到的错误消息new AVCaptureFileOutputRecordingDelegate
中创建Cannot create an instance of the abstract class or interface "AVFoundation.AVCaptureFileOutputRecordingDelegate"
时遇到了问题,我在代码中已经注释了您可以找到的内容:
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;
}
}
所以我的问题是如何解决这个AVCaptureFileOutputRecordingDelegate问题?
答案 0 :(得分:2)
那是因为AVCaptureFileOutputRecordingDelegat具有抽象函数FinishedRecording。你不能创建具有抽象的类的实例。您需要创建AVCaptureFileOutputRecordingDelegat的子类并在使用之前实现抽象方法。
像这样创建子类:
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();
}
}
现在,创建此类的实例:
AVCaptureFileOutputRecordingDelegate avDel= new myAvCaptureFileOutPutRecordingDelegate();