gpt4 book ai didi

ios - 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x48)

转载 作者:行者123 更新时间:2023-11-28 10:05:06 29 4
gpt4 key购买 nike

我在 XCODE 上使用 swift。我遇到了这个问题,如果我想点击我的按钮来播放我的声音。它会崩溃。我有 View Controller 。第一个 View Controller 连接到一个 Storyboard。那个 Storyboard有一个按钮播放声音。当我运行它时,声音会播放。当它转到第二个 Storyboard播放声音时它崩溃了

[2张图片是我用来播放音频的代码。我在第一个 View Controller 上粘贴了相同的代码。] 这是第二个 View Controller 上的内容。当我按下第二个 Storyboard上的按钮时,我得到了 Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) 但在我按下的第一个按钮上没有出现此错误。

当我运行模拟器并到达第二个 View Controller 时会发生这种情况

First

Second

class Level1ViewController: UIViewController, AVAudioPlayerDelegate {

var player: AVAudioPlayer = AVAudioPlayer()
var correctPlayer: AVAudioPlayer = AVAudioPlayer()
var wrongPlayer: AVAudioPlayer = AVAudioPlayer()




@IBAction func correctAnswer(_ sender: Any) {
correctPlayer.play()
}

@IBAction func playOne(_ sender: Any) {
player.play()
}
@IBOutlet weak var option2: UIButton! // wrong answer

@IBAction func wrongAnswer1LV(_ sender: Any) {
}


@IBOutlet weak var option1: UIButton!

override func viewDidLoad() {
super.viewDidLoad()


//View Controller 1 - Working

do {
let audioPlayer = Bundle.main.path (forResource: "bark", ofType: "wav")

try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer!)as URL)
}

catch {
//ERROR!
}

do {
let audioPlayer2 = Bundle.main.path (forResource: "Ding-Sound-Effect_qZC5gtOw3DU", ofType: "mp3")

try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)
}

catch {
//ERROR!
}

View Controller 2 - 不工作(给出错误)

class Level2LevelViewController: UIViewController, AVAudioPlayerDelegate {

var player: AVAudioPlayer = AVAudioPlayer()
var correctPlayer: AVAudioPlayer = AVAudioPlayer()
var wrongPlayer: AVAudioPlayer = AVAudioPlayer()

@IBAction func levelTwoPLay(_ sender: Any) {
correctPlayer.play() **** THREAD 1: EXC_BAD_ACESS****
}



override func viewDidLoad() {
super.viewDidLoad()


do {
let audioPlayerII = Bundle.main.path (forResource: "Door Open and Close Slam Sound Effects", ofType:"mp3")

try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayerII!)as URL)
}

catch {
//ERROR!!
}

leveltwoOp1.backgroundColor = UIColor.black
leveltwoOp2.backgroundColor = UIColor.black
leveltwoOpTh.backgroundColor = UIColor.black
leveltwoOp4.backgroundColor = UIColor.black

最佳答案

您正在 View Controller 1 中创建一个 correctPlayer 对象,并使用以下代码对其进行初始化:

try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)

没关系....但是在 View Controller 2 中,您有:

var correctPlayer: AVAudioPlayer = AVAudioPlayer()

它创建了一个 correctPlayer实例,它只在 View Controller 2 的范围内,然后您调用:

correctPlayer.play()

没有初始化它。

在 View Controller 2 中你需要再次调用:

    do {
let audioPlayer2 = Bundle.main.path (forResource: "Ding-Sound-Effect_qZC5gtOw3DU", ofType: "mp3")

try correctPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer2!)as URL)
}

catch {
//ERROR!
}

之后您应该可以播放该声音。

关于ios - 线程 1 : EXC_BAD_ACCESS (code=1, 地址=0x48),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123715/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com