Ios AVAudioPlayer不再在Swift 2.0/Xcode 7 beta版中工作
对于我的iPhone应用程序中的Ios AVAudioPlayer不再在Swift 2.0/Xcode 7 beta版中工作,ios,xcode,beta,xcode7,Ios,Xcode,Beta,Xcode7,对于我的iPhone应用程序中的var testAudio声明,我在这里收到一个错误 调用可以抛出,但不能从属性初始值设定项中抛出错误 这是在我升级到Xcode 7测试版时发生的 如何使此音频剪辑在Swift 2.0中运行?Swift 2有一个全新的错误处理系统,您可以在此处阅读更多信息: 在您的情况下,AVAudioPlayer构造函数可能会抛出错误。Swift不允许您使用在属性初始值设定项中抛出错误的方法,因为在那里无法处理它们。相反,在视图控制器的init启动之前,不要初始化属性 var
var testAudio
声明,我在这里收到一个错误
调用可以抛出,但不能从属性初始值设定项中抛出错误
这是在我升级到Xcode 7测试版时发生的
如何使此音频剪辑在Swift 2.0中运行?Swift 2有一个全新的错误处理系统,您可以在此处阅读更多信息: 在您的情况下,
AVAudioPlayer
构造函数可能会抛出错误。Swift不允许您使用在属性初始值设定项中抛出错误的方法,因为在那里无法处理它们。相反,在视图控制器的init
启动之前,不要初始化属性
var testAudio:AVAudioPlayer;
init() {
do {
try testAudio = AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("testAudio", ofType: "wav")!), fileTypeHint:nil)
} catch {
//Handle the error
}
}
这使您有机会处理创建音频播放器时可能出现的任何错误,并将停止Xcode向您发出警告。如果您知道不会返回错误,您可以添加并重试!事先:
testAudio = try! AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource
在Swift 2.2中为我工作 但是不要忘记将fileName.mp3添加到项目构建阶段->复制捆绑资源(右键单击项目根目录)
我相信,;此处不需要。如何获得AVAudioPlayer抛出的实际错误?
testAudio = try! AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource
var player = AVAudioPlayer()
func music()
{
let url:NSURL = NSBundle.mainBundle().URLForResource("fileName", withExtension: "mp3")!
do
{
player = try AVAudioPlayer(contentsOfURL: url, fileTypeHint: nil)
}
catch let error as NSError { print(error.description) }
player.numberOfLoops = 1
player.prepareToPlay()
player.play()
}