import UIKit import AudioToolbox//导入音频工具箱框架,这样就可以使用系统声音服务了 class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var _soundId:SystemSoundID = 0//声明一个系统声音标识的声音变量 let path = Bundle.main.path(forResource: "sheep", ofType: "mp3")//获取沙箱目录中,声音文件的所在路径 let soundUrl = URL(fileURLWithPath: path!)//将字符串路径转换成网址路径 //对于按键音、下拉菜单音等较短暂的声音,以及震动效果,可以使用系统音频服务来播放 AudioServicesCreateSystemSoundID(soundUrl as CFURL, &_soundId) AudioServicesPlaySystemSound(_soundId) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }