Hi there,
Has something changed with AVAudioPlayer with new Swift/SwiftUI version or when building for iOS 15?
This piece of code below used to work without issues when building the app for iOS 14. I use AVAudioPlayer
to listen to a preview track from iTunes, e.g. https://audio-ssl.itunes.apple.com/itunes-assets/AudioPreview115/v4/7c/33/61/7c33611a-0fc8-546b-d737-287e75867ddb/mzaf_7738318112069179681.plus.aac.p.m4a.
However, I am not hearing any sound on a test device on which I reinstalled the app built with iOS 15 (rather than iOS 14). I did use the exact same code and I am referring to the same URL...
Both devices are running iOS 15.3.
In short: with production app (built with deployment target iOS 14.x) I can hear sound; test app (built with deployment target iOS 15.3) I cannot hear sound...
Code is like
class PreviewTrack {
static var audioPlayer: AVAudioPlayer!
static func playTheOldWay(sound: String, status: Bool) {
let previewURL = URL(string: sound)
print("Preview Track URL: \(sound)")
let task = URLSession.shared.dataTask(with: previewURL!) { data, _, error in
guard let _ = data,
error == nil else {
Logger.viewCycle.info("PreviewTrack - \(error?.localizedDescription ?? "Response Error")")
return }
if let data = data {
self.audioPlayer = try? AVAudioPlayer(data: data)
if status {
Logger.viewCycle.info("PreviewTrack - Playing Track...")
self.audioPlayer.play()
}
else {
Logger.viewCycle.info("PreviewTrack - Stoppimng Track...")
self.audioPlayer.stop()
}
}
}
task.resume()
}
}