Tuesday, 5 June 2018

Playing mp3/wav sound on iOS 9 causes exception

When playing audio using AVAudioPlayer, be it mp3 or wav, I'm getting an exception on all iPads and iPhone 4S and only iOS 9. This does not happen on any other devices.

The exception arises from

com.apple.coreaudio.AQClient (18): breakpoint 2.2

from

libc++abi.dylib`__cxa_throw:

and I'm catching it using a user breakpoint. $arg1 does not contain anything

This is how I'm initializing AVAudioPlayer

guard let path = Bundle.main.path(forResource: file, ofType: type) else { return nil }
guard let url = URL(string: path) else { return nil }

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true)

return try? AVAudioPlayer(contentsOf: url)

I'm playing sound from an async global dispatch queue and the method playing the sound contains

self.sound?.prepareToPlay()
self.sound?.play()

Initially the file was a wav file, so I converted it to a mp3 file, which didn't help. I've also used a totally different sound, which produces the same exceptions.

The exception itself doesn't crash the simulator, although Rollbar reports SIGSEGV errors with such stracktrace:

libAVFAudio.dylib in 0x27c1c000

Audio class used:

import AVFoundation
import Foundation

class Audiotools {
    var sound: AVAudioPlayer?

    let dispatchQueue = DispatchQueue(label: "audio")

    static var shared: Audiotools = {
        let manager = Audiotools()
        return manager
    }()

    init() {
        self.sound = configurePlayer(withFile: Files.sound.name, type: Files.sound.extension)
    }

    func configurePlayer(withFile file: String, type: String) -> AVAudioPlayer? {
        guard let path = Bundle.main.path(forResource: file, ofType: type) else { return nil }
        guard let url = URL(string: path) else { return nil }

        try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, with: .mixWithOthers)
        try? AVAudioSession.sharedInstance().setActive(true)

        return try? AVAudioPlayer(contentsOf: url)
    }

    func playSound() {
        self.dispatchQueue.async {
            self.sound?.prepareToPlay()
            self.sound?.play()
        }
    }
}

Has anyone else had this kind of problem?



from Playing mp3/wav sound on iOS 9 causes exception

No comments:

Post a Comment