Search code examples
iosswiftxcodeavaudioplayericloud

AVPlayer / AVAudioPlayer - Play audio fileURL from iCloud Drive (work on simulator but not iPhone) + (OSStatus error -54.)


I'm making Audio Player. Importing file from iCloud Drive using .fileImporter.

I get file URL that looks like this: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/_Storage/Audio-books/%D0%91%D1%80%D0%B5%D0%BD%D0%B4%D1%8F%D1%82%D0%B8%D0%BD%D0%B0/Audiobook.mp3"

Then I pass it to audio player (tried AVPlayer and AVAudioPlayer). Both works on iOS simulator. On the device after import I get error: The operation couldn’t be completed. (OSStatus error -54.)

I know it's possible, app called Evermusic does quite the same with on device files.

  • Is there permissions I need to be granted to play audio that stored on device?
  • How can I access Container for com~apple~CloudDocs?

Thank you very much for help, any suggestions greatly appreciated, I'm seriously stuck. For future references repo of the project: https://github.com/yaosamo/AudioPlayer


Solution

  • You need to be using startAccessingSecurityScopedResource in order to get read access to those files. See documentation: