ios - 在新的 iOS 版本中,AVAudioPlayer 数组出现错误访问错误,但为什么

标签 ios swift avaudioplayer

我使用以下函数来准备多个音频播放器。

但是,当在新的 iOS 版本上运行代码时,我从行获取Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) 玩家[i] =玩家

它仍然可以在旧的 iOS 版本上运行,没有问题。

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 :( ")
    }
 }

最佳答案

在使用上面的函数之前,我正在创建一个这样的数组。 我就是这样做的

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

因为有时我会改变名字 播放器的音频。为了轻松做到这一点,我分离了音频加载功能。

现在,我正在创建这样的玩家数组。 并在相关功能的修订版本中更改了音频名称。

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"}
    }
}

关于ios - 在新的 iOS 版本中,AVAudioPlayer 数组出现错误访问错误,但为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58745418/

相关文章:

ios - 如何将我的当前位置分享给 UIActivityViewController?

ios - -[PUPhotosGridViewController assetAtIndexPath :] NSRangeException

iphone - 无法在 ScrollView 中显示 ScrollView 指示器

swift - 我如何优化这段操作字符串的代码?

iphone - 如何在iPhone App中添加音频播放器

ios - 如何在 ionic 框架中为 ios 制作复选框?

ios - 自调整 UILabel 与 UIView 子类化

ios - 在文本中添加无限行(SwiftUI)

swift - 使用 slider 解析音频

iphone - 如何让 AVAudioPlayer 输出到扬声器