1

My app lets users select an audio recording from their iTunes media library, then saves the media ID for later playback. From the media ID, I can use MPMediaQuery to get an MPMediaItem, then get the recording URL:

let recordingURL = mediaItem?.value(forProperty: MPMediaItemPropertyAssetURL) as? URL

From the URL, I create an AVAudioFile that I can give to an AVAudioPlayerNode for playback:

do {
    let audioUnitFile = try AVAudioFile(forReading: recordingURL)
} catch let error {
    NSLog("error loading AVAudioFile: " + error.localizedDescription)
}

This all works fine on iOS. On Mac Catalyst, I can get a valid URL (I can see it's correct when inspecting it in the debugger), but the AVAudioFile:forReading function throws this error:

ExtAudioFile.cpp:193:Open: about to throw -54: open audio file
[avae]            AVAEInternal.h:109   [AVAudioFile.mm:134:AVAudioFileImpl: (ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)): error -54
error loading AVAudioFile: The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error -54.)

I checked the Privacy settings in the Mac Settings app, and my app has permission to access the music library. What else can I check? What does error -54 mean? Is this supported with Mac Catalyst apps?

I'm using Xcode 12.5.1 on macOS 11.4.

arlomedia
  • 8,534
  • 5
  • 60
  • 108

1 Answers1

1

Oh, I just found the problem. I needed to do:

recordingURL?.startAccessingSecurityScopedResource()
let audioUnitFile = try AVAudioFile(forReading: recordingURL)
recordingURL?.stopAccessingSecurityScopedResource()

I'm doing this with other files, but for some reason I didn't think to do it with media library files.

arlomedia
  • 8,534
  • 5
  • 60
  • 108