19

My App is crashing on iOS 13.1 when i trace a issue then found App is crashing because of AVAudioPlayer.

Below Is My Player Setup.

if let wrongURL = Bundle.main.url(forResource: "wrongAudio", withExtension: "mp3")
        {
            do {
                wrongMusicPlayer = try AVAudioPlayer(contentsOf: wrongURL)
                wrongMusicPlayer.prepareToPlay()
            }
            catch
            {
                print(error.localizedDescription)
            }
        }

Here All things found correctly but crash when AVAudioPlayer try to initialise with URL.

Rakesh Patel
  • 1,673
  • 10
  • 27

4 Answers4

42

I found a crash issue in AVAudioPlayer with iOS 13.1.

Here Is Solution

Why My AVAudioPlayer crash? because

I initialise AVAudioPlayer like

var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer()

and then i try to reassign wrongMusicPlayer as below

wrongMusicPlayer = try AVAudioPlayer(contentsOf: wrongURL)

And my app get crash.

Solution

If you initialise your AVAudioPlayer like var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer() OR wrongMusicPlayer = AVAudioPlayer() in any method then please remove it and just Declare like var wrongMusicPlayer: AVAudioPlayer!.

Rakesh Patel
  • 1,673
  • 10
  • 27
3

iOS 13.2, which be released today, fixes this issue.

  • I didn't experienced the issue of a crash on iOS 13.2 but on objective c if you do a alloc init first, then on other line you do a avaudioplayer initwith, the object that had the audioplayer initialized was set to nil and the additional init was not working at all, so I had to change to use alloc and initWithData or initWithContentsOfURL to be used immediately after the alloc. – Alfonso Nava Nov 03 '19 at 16:25
  • updating to ios 13.2 and XCode 11.2 fixed this issue – ciara staggs Nov 04 '19 at 19:05
2

Thanks, Rakesh! It's amazing! My game broke on iPhone with iOS 13.1.2. But in simulator it's work. On physical device - no! I read a lot of forums, online books, docs, etc.

And this small fix really helped me: I changed var player = AVAudioPlayer() to var player: AVAudioPlayer!

p.s. guys wrote about problem with threads but this case is simpler

Eugene
  • 221
  • 3
  • 6
1

The same issue happens with Objective C code.

Previously musicPlayer = [[AVAudioPlayer alloc] init]; would work but will now cause a crash in iOS13. The crash happens later on when using one of the other init methods like initWithContentsOfURL.

Removing the [[AVAudioPlayer alloc] init] fixes the issue.

omanosoft
  • 4,239
  • 1
  • 9
  • 16
Grant Luck
  • 79
  • 3