当我将应用程序从Xcode运行到iPhone7时,它在以下行崩溃:
audioNSURL NSURL“ file:///private/var/containers/Bundle/Application/016483D7-D06F-4639-A026-CCD0B1F4B31F/Solo%20Mission.app/spaceMusic.mp3” 0x000000028217eac0
与GameViewController内部的AVAudioPlayer有关,这给了我此错误消息:
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48)
请帮助解决这个问题,我已经检查了构建阶段,并且文件在那里。下面是所有代码。
import UIKit
import SpriteKit
import AVFoundation
class GameViewController: UIViewController {
var backingAudio = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let filePath = Bundle.main.path(forResource: "spaceMusic", ofType: "mp3")
let audioNSURL = NSURL(fileURLWithPath: filePath!)
do { backingAudio = try AVAudioPlayer(contentsOf: audioNSURL as URL) }
catch { return print("Cannot Find The Audio") }
backingAudio.numberOfLoops = -1
backingAudio.volume = 1
backingAudio.play()
答案 0 :(得分:0)
已解决!
我尝试了许多不同的方法,但最终我解决了这个问题。我只需要编写不同的代码。这是我写的:
var player: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
func initializePlayer() -> AVAudioPlayer? {
guard let path = Bundle.main.path(forResource: "spaceMusic", ofType: "mp3") else {
return nil
}
return try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
}
player = initializePlayer()
player?.play()
说实话,我不太明白为什么这样做。但是,我将不胜感激。