Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashes when playing audio on iOS13.1

I am building an app that runs sound files from within the main bundle with a url. When I tested this on iOS 13, everything is fine. But with the new update of 13.1 I am getting an error here on the line of code

backgroundMusicPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))

that says:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x48

Here is the code that I am using in a custom class that runs background music when the app launches:

import Foundation
import AVFoundation

var backgroundMusicPlayer = AVAudioPlayer()

func playBackgroundMusic(filename: String){
let  sound = Bundle.main.path(forResource: filename, ofType: "m4a")

do{
    try     
AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [AVAudioSession.CategoryOptions.mixWithOthers])
    backgroundMusicPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))

}catch{
    print (error)
}
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
}

This all works fine in the simulator on iOS13, but crashes on a device running 13.1 It appears that the url is the issue, but I am not sure why. This same behavior happens on other screens where buttons are triggering audio files from the bundle.

like image 216
Terry B Avatar asked Sep 25 '19 19:09

Terry B


People also ask

Why does my apps keep crashing iOS 13?

Third solution: Install pending app updates on your Apple iPhone. Apps that are more prone to crash or go rogue from an iOS update are usually those that are outdated. This happens because the minimum system requirements for the application to work with your iPhone are no longer met.

What causes apps to crash on iOS?

Most often, apps crash because they aren't updated and have unaddressed bugs. To fix this, go to the Updates section of the App Store and update the app if an update is available.

Why are my apps crashing randomly?

Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.

What should I do if an app keeps crashing?

The easiest way to fix an app that keeps crashing on your Android smartphone is to simply force stop it and open it again. To do this, go to Settings -> Apps and select the app that keeps crashing. Tap on the app's name and then tap on 'Force stop'. Now try opening the app again and see if it works well.


2 Answers

Change this:

var backgroundMusicPlayer = AVAudioPlayer()

To this:

var backgroundMusicPlayer : AVAudioPlayer!
like image 68
もぶわさお Avatar answered Sep 20 '22 03:09

もぶわさお


AVAudioPlayer doesn't have an init so it should be removed.

Solution for swift

If you initialise your AVAudioPlayer like:

var musicPlayer: AVAudioPlayer = AVAudioPlayer() 

or

musicPlayer = AVAudioPlayer() 

in any method then remove it and declare like:

var musicPlayer: AVAudioPlayer!

Solution for Objective C

If you initalise like

AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] init];

REMOVE the init part of [[AVAudioPlayer alloc] init] to look like

AVAudioPlayer *musicPlayer = [AVAudioPlayer alloc];

EDIT: If after this, your app pauses at that line, like you set the breakpoint there(but you didn't), but app run without problems after you click play/run, you shouldn't worry because it is some c level issue that doesn't affect the app. You can read more in this thread So solution for that is to edit the breakpoint for All Exception, change the exception type from "All" to "Objective-C exceptions"

  1. Go to the Breakpoint navigator in Xcode.
  2. Control-click on the 'All Exceptions' line.
  3. Select the 'Edit Breakpoint...' option.
  4. Change the Exception from All to Objective-C.

enter image description here

like image 38
omanosoft Avatar answered Sep 22 '22 03:09

omanosoft