19

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.

Terry B
  • 266
  • 3
  • 11

3 Answers3

26

Change this:

var backgroundMusicPlayer = AVAudioPlayer()

To this:

var backgroundMusicPlayer : AVAudioPlayer!
Rich
  • 6,470
  • 15
  • 32
  • 53
もぶわさお
  • 276
  • 3
  • 2
  • 1
    Any solution for Objecitve C? – iOS.Lover Sep 30 '19 at 09:02
  • 1
    @Mc.Lover Remove all [[AVAudioPlayer alloc] init], and init the player when it is actually used, with a proper url. – nullforlife Oct 02 '19 at 09:17
  • This fixes the crash for me, but there is no sound. Does anyone else have the same problem? – nullforlife Oct 02 '19 at 09:24
  • I am having the same problem with this line of code always triggering a crash: self.alertSoundEffect = try AVAudioPlayer(contentsOf: url). Making the above change has had no effect on this crashing behavior. Are there any other possible solutions to the inability to run that line of code on a device running ios 13.1.2? – michaeldebo Oct 04 '19 at 09:47
  • I had the same problem as OP and making this change works perfectly, crash is gone and sound is OK. I am running Xcode 11.1 and iOS 13.1.2 tested on iPad Air 2 and iPhone 8 – mac_eric Oct 10 '19 at 06:54
  • Although this is the solution, the answer is very simple, without any explanation. ```omanosoft```'s answer is better, if you care to understand the problem, and not just google for a line of code to just copy/paste. – Starsky May 25 '20 at 11:52
7

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

omanosoft
  • 4,239
  • 1
  • 9
  • 16
0

Add below code in AppDelegate.swift

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSession.Category.playback)
    }
    catch {
        print("Setting category to AVAudioSessionCategoryPlayback failed.")
    }
    return true
}
LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57