iOS 14更新后,Xamarin相机渲染器无法录制视频

问题描述

我正在Xamarin Forms上编程,并创建了iOS渲染器。因此,在更新iOS 14之后,我的应用开始了录制,但之后立即停止了。在iOS 13上,记录运行良好。

在iOS 14上面对问题的步骤:

  1. 显示了摄像机预览。
  2. 用户单击开始记录按钮。
  3. 录制视频状态图标显示在iPhone顶部栏上(本机显示
  4. 捕获立即停止

在iOS 13上的步骤:

  1. 显示了摄像机预览。
  2. 用户单击开始记录按钮。
  3. 录制视频状态图标显示在iPhone顶部栏上(本机显示
  4. 5秒(我设置的时间限制)后,捕获停止。

下面是我正在使用的渲染器代码

 private void StartVideo(object sender,EventArgs e)
    {
        Element.completed = new taskcompletionsource<VideoViewResult>();

        NSUrl url = new NSUrl(PathFile,true);

        if (avDel == null) avDel = new AVCaptureFileOutputRecording();
        if (output == null) output = new AVCaptureMovieFileOutput();

        var stillImageOutput = new AVCaptureStillImageOutput()
        {
            OutputSettings = new NSDictionary()
        };

        output.MaxRecordedDuration = CoreMedia.CMTime.FromSeconds(5,1); // time limit
        // output.MaxRecordedDuration = CoreMedia.CMTime.FromSeconds(50000,1);  //not working too

        if (Control.CaptureSession.CanAddOutput(output)) Control.CaptureSession.AddOutput(output);

        output.StartRecordingToOutputFile(url,avDel);

        avDel.StopRecord += StopRecording;
    }

我已经分析了权限,但是还可以。有什么解决办法吗?谢谢。

解决方法

问题出在视频文件的位置。

旧代码:

private static string GetOutputPath()
{
    string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"MyApp");
    Directory.CreateDirectory(folder); 
            
    return Path.Combine(folder,$"{Guid.NewGuid().ToString("N")}.{VideoFormat}");
}

新代码:

private static NSUrl GetOutputPath()
{
   NSUrl url;

   if (UIDevice.CurrentDevice.CheckSystemVersion(14,0))
      url = NSFileManager.DefaultManager.GetTemporaryDirectory().Append($"{Guid.NewGuid():N}.{VideoFormat}",false);
   else
   {
      string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"MyApp");
      Directory.CreateDirectory(folder);

      var PathFile = Path.Combine(folder,$"{Guid.NewGuid():N}.{VideoFormat}");

      url = new NSUrl(PathFile,true);
    }

    return url;
}

现在可以正确录制视频了,

该应用程序具有用户权限(NSPhotoLibraryUsageDescription / NSPhotoLibraryAddUsageDescription)的对照片的完全访问权限

我开始使用一个临时文件夹,问题已解决。但是,最好像以前的iOS版本一样使用Personal目录(Environment.SpecialFolder.Personal)