Post not yet marked as solved
Click to stop watching this thread.
You have stopped watching this post. Click to start watching again.
contentPostList.repliesup-voted.tooltip
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
}
}
}
}
}
}
}