【发布时间】:2018-03-13 14:21:19
【问题描述】:
我是 Swift 的新手。我正在尝试运行此代码,但出现错误:
线程 1:EXC_BAD_ACCESS(代码=1,地址=0x48)
此处出现错误:
self.player.play()
谁能帮忙解决这个问题?
导入 UIKit 导入 AVFoundation类视图控制器:UIViewController {
var player = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
do {
if let audioPath = Bundle.main.path(forResource: "music", ofType: "mp3") {
try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath))
}
} catch {
print("ERROR")
}
self.player.play()
}
【问题讨论】:
-
我认为
let audiopath或try player没有成功,然后player.play()中断。 music.mp3 是否在正确的文件夹中?当 AVAudioPlayer 没有可播放的内容时,可能会抛出此错误。 -
嗨米格尔。它看起来像真理。 guard let audioPath = Bundle.main.path(forResource: "music", ofType: "mp3") else { print("Error 1") return } 返回错误 1。现在我将尝试获取正确的路径。