Ok, let me share my findings (no matter how scarce), may be they will help someone. I did not use AVAudioPlayer's delegate in my code, but I had a collection of sounds which were supposedly playing and once in a while I iterated over that collection and deleted sounds with false isPlaying field. It resulted in a crash mentioned above, though it was extremely rare. Your situation may look completely different (like having a single AVAudioPlayer which gets deleted together with encompassing class), but the effect is probably the same.
What I changed to finally get rid of the bug: I made AVAudioPlayerDelegate, which deleted the sound that just finished playing.
My theory is the following. The sound playing code runs on another thread and when it finishes, it sets isPlaying to false (or whatever results in it returning false) and tries to call the delegate's finishedPlaying on UI thread, which involves some minor delay. Now what happens if you delete the AVAudioPlayer between those 2 events is probably what results in attempt to access the deleted player's delegate?.finishedPlaying.
It is just a speculative guesswork, but it seems to fit the observed results: the crash, why it is so difficult to reproduce and why my changes fixed it.
Topic:
Media Technologies
SubTopic:
Audio
Tags: