0

I was using the following function to make multiple audio players ready.

But when run the code at new iOS Versions I am getting Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) from line players[i] = player

It still runs at old iOS versions without a problem.

func loadMusicToAPlayer(i: Int, name: String){
    let s = Bundle.main.path(forResource: name, ofType:nil)!
    let url = URL(fileURLWithPath: s )
    do {
        let player = try AVAudioPlayer(contentsOf: url)
        players[i] = player
        players[i].prepareToPlay()
        players[i].isMeteringEnabled = true
    } catch {
        print("couldn't load music file :( ")
    }
 }
Maciej Gad
  • 1,701
  • 16
  • 21
Hope
  • 2,096
  • 3
  • 23
  • 40
  • show your code where you create players property – kirander Nov 07 '19 at 09:25
  • I am creating player array like that before hand, **var players : [AVAudioPlayer] = []**, **players.append(AVAudioPlayer())**, as much as I need in a loop. – Hope Nov 07 '19 at 09:27
  • `AVAudioPlayer()` crash on early version of iOS13. https://stackoverflow.com/questions/58110827/ios-13-1-crash-in-avaudio-player https://stackoverflow.com/questions/58144735/avaudioplayer-crashes-on-ios-13 etc. – Larme Nov 07 '19 at 09:32
  • Yeah I notice that if I create players like **players.append(player)** it works but I have to change heck of a code now. Because I was calling function from multiple places. – Hope Nov 07 '19 at 09:36

1 Answers1

0

I was creating an array like this before using the function above. I was doing it like this

for k in 0..< audioList.count {
    players.append(AVAudioPlayer())
}

because sometimes I was changing the name of audio of a player. To do it easily I was separated audio loading function.

Now, I am creating player array like this. And changed audio name in revised version of function in question.

func loadAudio(name: [String]){

    for k in 0..< name.count {
        let s = Bundle.main.path(forResource: name[k], ofType:nil)!
        let url = URL(fileURLWithPath: s )

        do {
            let player = try AVAudioPlayer(contentsOf: url)
            players.append(player)
        } catch { print("could not load audio file"}
    }
}
Maciej Gad
  • 1,701
  • 16
  • 21
Hope
  • 2,096
  • 3
  • 23
  • 40