gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value using AVAudio

转载 作者:可可西里 更新时间:2023-11-01 00:53:32 24 4
gpt4 key购买 nike

我有两个项目,使用相同的代码,但下面一行:

player = AVAudioPlayer(contentsOfURL: NSURL(string: fileLocation), error: &error)

仅在一个项目中返回此错误:

fatal error: unexpectedly found nil unwrapping an Optional value

另一个项目运行没有错误。

我正在尝试播放声音。

import UIKit
import AVFoundation


class ViewController: UIViewController {


var player:AVAudioPlayer = AVAudioPlayer()

override func viewDidLoad() {
super.viewDidLoad()
var fileLocation = NSString(string: NSBundle.mainBundle().pathForResource("halloweenmp3", ofType: "mp3")!)

var error: NSError? = nil

player = AVAudioPlayer(contentsOfURL: NSURL(string: fileLocation), error: &error)

player.play()


}

在两个项目中,相同的 mp3 文件已添加到项目中,但一个项目将播放声音,而另一个项目返回错误。

给我错误的项目中确实有其他代码,但错误似乎只与上面提到的行有关,所以我看不出项目中的其他代码是如何导致它的。

我已经尝试复制容易出错的项目并得到相同的结果。我创建了新项目并使用了上面使用的相同代码并让它工作。我不确定项目是否以某种方式损坏或项目中的另一位代码导致了错误,即使它没有在调试器中被引用。

我可以在 xCode 的文件导航器中预览 mp3 文件,所以它可以正常工作。

最佳答案

NSURL(String:) 更改为 NSURL(fileURLWithPath:) 对我有用。

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value using AVAudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650876/

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