Search code examples
delphiaudiodelphi-10.4-sydney

Delphi MacOS project gets Error "Unauthorized to record audio"


A new Delphi 10.4.2 macOS project attempts to record microphone audio but gets this error message in a pop up window:

"Unauthorized to record audio."

How does a Delphi app get authorization to record audio?

I started with blank project so...

Project | Options | Application | Version Info | Key NSMicrophoneUsageDescription is set to the default string of "The reason for accessing the microphone"

The exception is being raised here in FMX.Media.AVFoundation:

    {$ELSEIF DEFINED(MACOS)}
  if TOSVersion.Check(10, 14) and (TAVCaptureDevice.OCClass.authorizationStatusForMediaType(AVMediaTypeAudio) <> AVAuthorizationStatusAuthorized) then
    raise ECaptureDeviceException.Create(SAudioCaptureUnauthorized);
{$ENDIF}

Solution

  • Adding the call to RequestPermission worked.

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      fMic := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
    
      {$IFDEF MACOS}
      fMic.RequestPermission;
      {$ENDIF}
    end;