Xamarin camera renderer can't recording video after iOS 14 update

Viewed 185

I'm programming on Xamarin Forms and created a renderer to iOS. So after iOS 14 update, my app starts the recording but stops immediatly after. On iOS 13 the record runs perfectly.

Steps to face the problem on iOS 14:

  1. Camera preview is showed.
  2. The user clicks on start record button.
  3. The recording video status icon is shown on iPhone top bar (native)
  4. The capture is stopped immediatly

Steps on iOS 13:

  1. Camera preview is showed.
  2. The user clicks on the start record button.
  3. The recording video status icon is shown on iPhone top bar (native)
  4. The capture is stopped after 5 seconds (time limit that I set).

Below is the renderer code, that I'm using:

 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;
    }

I've analyzed the permissions, but kinda OK. Any ideas to solve this? Thanks.

1 Answers

The problem was in the location of the video file.

Old code:

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}");
}

New code:

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;
}

Now the video is recorded correctly,

The application has full access to Photo with the user's permission (NSPhotoLibraryUsageDescription/NSPhotoLibraryAddUsageDescription)

I started using a temporary folder and my problem was resolved. However, it would be good to work with the Personal directory (Environment.SpecialFolder.Personal) as in previous versions of iOS

Related