The following code works for audio files of short duration on all my devices (iPhone, Apple Watch, Simulator):
let file = try! AVAudioFile(forReading: url!)
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
let buf = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: UInt32(file.length))
try! file.read(into: buf!)
The crash occurs at try! file.read(into: buf!)
However trying to read a 5 min for example long audio file is causing a crash running on my Apple Watch (though works on the Apple Watch Simulator). With Program ended with exit code: 0
.
Watching the debug tools, obviously this process is a lot more taxing on the Apple Watch than an iPhone. I've tried to run it on different threads, background, etc to no avail.
I know I could send the buffer to the iPhone for processing, however I'd like to see (even if it takes a little longer) if I can read into the buffer strictly from the watch.
5 minutes of mono LPCM float32 audio at 44.1kHz would be 5*60*44100*4 bytes, or about 53MB. Double that for stereo. I'm don't know what the watch's memory limits are, but I think you're exceeding them.