2

When I am trying to run my code, I get the error in the title above in my code on the line that says

audioPlayer = try AVAudioPlayer(contentsOf: url)

This code worked like a charm before I updated my Xcode into Xcode 11, and now with swift 5, I had to change it and now it doesn't work. can someone please help me? Here is my code

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    playAudio()

}

@IBAction func playButton(_ sender: Any) {
playAudio()
}


var indexPath = 0
var listOfTheSongs = ["badLiar.mp3", "cool.mp3", "eastSide.mp3", "fallinAllInYou.mp3", "hateHowMuchILoveYou.mp3", "inMyBlood.mp3", "letMeDownSlowly.mp3", "loveYourself.mp3", "panini.mp3", "rewriteTheStars.mp3", "stiches.mp3", "treatYouBetter.mp3", "wow.mp3"]
 var audioPlayer = AVAudioPlayer()
 var path = Bundle.main.path(forResource: "badLiar.mp3" , ofType: nil , inDirectory: "Songs For Lyric Hunt")!


func playAudio () {

    if path != nil {
        print("path does not equal nil")
        let url = URL(fileURLWithPath: path)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            if audioPlayer.isPlaying == true {
            audioPlayer.stop()
            } else {
                audioPlayer.play()
            }
        } catch {
            // couldn't load file :(
            print("counldn't load file :(")
        }
    }else {
        print(" path equals to nil")

        }
    }



}
  • Did you check whether 'url' is properly initialized? Check that with a breakpoint or print. EXC_BAD_ACCESS is mostly because of attempt to access invalid memory. – geet Sebastian Oct 09 '19 at 23:41
  • can you please specify exactly how to check that, please? –  Oct 10 '19 at 00:16
  • Just print("url value: \(url)"); before 'do' to print. Or else, place a breakpoint on the line audioPlayer = try AVAudioPlayer(contentsOf: url), and when it breaks in the console, "po url". Check what is the value of url. Most probably it will be something invalid. – geet Sebastian Oct 10 '19 at 00:36
  • the result of the printing the url was "file:///Users/ryan/Library/Developer/CoreSimulator/Devices/541EC05F-C569-4049-8F38-7CA1D919C72C/data/Containers/Bundle/Application/3C1D3317-946A-4FC0-8285-476A0FA644E7/Lyric%20Hunt.app/Songs%20For%20Lyric%20Hunt/badLiar.mp3", does that seem invalid? –  Oct 10 '19 at 00:51

2 Answers2

1

Referring to this answer, you need to initialize your AVAudioPlayer like below:

var audioPlayer: AVAudioPlayer!

Replace your var audioPlayer = AVAudioPlayer() with the above line.

This should fix the crash. Seems that the problem is happening from iOS 13.1

geet Sebastian
  • 677
  • 6
  • 12
  • Thank you. After I formatted my Mac and installed OSX Catalina and reinstalled Xcode updating to 11.1, on running my project I got this error > Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) At first I had these little ? marks next to the sound file in Xcode which said 'unversioned' when I hovered the mouse over it. I right clicked it and chose 'Source control' - 'Add' and changed to a little A (added) and it fixed. But I was still getting the error until I changed `var audioPlayer = AVAudioPlayer()` to what you suggested `var audioPlayer: AVAudioPlayer!` – Kurt L. Oct 13 '19 at 06:26
0

Did you add the framework to your project? if not, try this

in the General tab of the game/App Got to Frameworks, Libraries, And Embedded Content click on the + and type or search for AVFoundation.framework

hope it helps