我正在尝试播放警报声音,但当我实现代码时,它会运行,但没有播放任何声音。有什么问题吗?
以下是我的代码(重要部分):
import UIKit
//Timer Sound
let timerSoundURL = NSBundle.mainBundle().URLForResource("alarm", withExtension: "caf")
var timerSoundID: SystemSoundID = 0
override func viewDidLoad() {
//Setup sound for timer
AudioServicesCreateSystemSoundID(timerSoundURL, &timerSoundID)
}
//Plays sound
override func viewDidAppear(animated: Bool) {
AudioServicesPlaySystemSound(timerSoundID);
NSLog("Play sound")
}发布于 2015-03-30 01:14:12
您的代码看起来很好,但您可能正在模拟器中对其进行测试。如果是这样,那就是你的错误。AudioServicesPlaySystemSound在模拟器中不起作用。
在实际的设备上运行你的代码,一切都会很好(除了你有一个内存泄漏,但这完全是另一回事-关键是,当你在设备上运行时,如果资源是有效的,你会听到声音)。
https://stackoverflow.com/questions/29331604
复制相似问题