gpt4 book ai didi

ios - Swift AVAudioPlayer 不播放声音

转载 作者:可可西里 更新时间:2023-11-01 02:26:08 26 4
gpt4 key购买 nike

我想在我的 Swift 应用程序中播放两个声音文件。 “C1_bip.aif”和“false.aif”。它们都在项目的同一个文件夹中。

但是当我启动应用程序时,会播放 C1_bip 声音而不会播放错误声音,并且它会向控制台打印错误。

声音文件是相同的文件类型,我可以在 Xcode 的项目资源管理器中播放它们。

if let path = NSBundle.mainBundle().pathForResource("C1_bip", ofType: "aif") {
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path),
fileTypeHint: "aif", error: nil)

if let sound = audioPlayer {

sound.prepareToPlay()
sound.play()
}
}
else {
println("Error")
}



if let path = NSBundle.mainBundle().pathForResource("false", ofType: "aif") {
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path),
fileTypeHint: "aif", error: nil)

if let sound = audioPlayer {

sound.prepareToPlay()
sound.play()
}
}
else {
println("Error")
}

最佳答案

这是我在测试项目中所做的:

    let musicPath = NSBundle.mainBundle().pathForResource("bgmusic", ofType: "mp3")
let url = NSURL(fileURLWithPath: musicPath!)

do{
audioPlayer = try AVAudioPlayer(contentsOfURL: url)
}catch _ {
audioPlayer = nil
}
audioPlayer.numberOfLoops = -1
audioPlayer.prepareToPlay()
audioPlayer.play()

而且效果很好:)

关于ios - Swift AVAudioPlayer 不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896934/

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