今天拖了一个声音文件到项目中
然后通过以下代码加载
func playAlertSound() {
guard let soundPath = Bundle.main.path(forResource: "noticeMusic", ofType: "caf") else { return }
guard let soundUrl = NSURL(string: soundPath) else { return }
var soundID: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl, &soundID)
AudioServicesPlaySystemSound(soundID)
}
但是第一句的时候soundPath为空就直接返回了.挠头半天也找不到原因, 后来还是stackoverflow帮忙解决了,如下图
在CopyBundleResources里把声音文件加进去就好啦.
嗯,这个声音格外好听.