Can no longer access iTunes music purchases with AVAudioFile with iOS 17

Prior to iOS 17, I used AVAudioFile to open (for reading) the assetURL of MPMediaItem for songs that the user purchased through iTunes Store. With the iOS 17 Beta, this seems no longer possible as AVAudioFile throws this:

ExtAudioFile.cpp:211 about to throw -54: open audio file
AVAEInternal.h:109 [AVAudioFile.mm:135:AVAudioFileImpl: (ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)): error -54

Also can't copy the url to Documents directory because I get this:

The file “item.m4a” couldn’t be opened because URL type ipod-library isn’t supported.

This seems to be affecting other apps on the App Store besides mine, and it will reflect very badly on my app if this makes into the final iOS 17 because I have encouraged users to buy songs on iTunes Store to use with my app. Now there seems like there is no way to access them.

Is this a known bug, or is there some type of workaround?

Post not yet marked as solved Up vote post of oneway111 Down vote post of oneway111
1.2k views

Replies

Here's a code example, will print the error on iOS 17, but print success and start playing audio on iOS 16:

import SwiftUI
import MediaPlayer
import AVFoundation

var player: AVAudioPlayer?

struct ContentView: View {
    @State var success = ""
    var body: some View {
        VStack {
            Text(success)
        }
        .padding()
        .onAppear {
            MPMediaLibrary.requestAuthorization { (status) in
                    if status == .authorized {
                        print("Access granted")
                        let songs = MPMediaQuery.songs()
                        let playableSong = songs.items?.filter { song in
                            song.assetURL != nil && !song.hasProtectedAsset && song.assetURL!.absoluteString.contains("m4a")
                        }.first
                        if let playableSong {
                            do {
                                let file = try AVAudioFile(forReading: playableSong.assetURL!)
                                player = try? AVAudioPlayer(contentsOf: playableSong.assetURL!)
                                player?.play()
                                success = "success"
                            } catch {
                                print(error)
                                success = error.localizedDescription
                            }
                        }
                    }
                }
        }
    }
}

Facing the same issue. It has been a problem for a few betas now. Not sure if it will ever be fixed.

We are experiencing the same problem.

My app uses the low level Extended Audio File Services and exhibits this same iOS 17 issue when I call the ExtAudioFileRead function to get the first sample from the m4a file url of an iTunes purchased or uploaded song file. The error code returned is 1 and the device log has this line "ExtAudioFile.cpp:1,187 about to throw 1: convert audio packets (pcm read)" All other extaudioxxxx calls seems to work normally, but the inability to read the samples from the aac audio format converted to pcm prevents playback.

Facing the same problem! Pretty sure this will be affecting tons of apps (i.e using AVAudioEngine/AudioKit)

Good news! The issue seems to have been fixed in Beta 8. FINALLY!