swift4--使用SystemSoundld播放简短声音

141 阅读1分钟
import AudioToolbox
var soundId:SystemSoundID = 1

//获取沙箱目录中文件所在的路径 waw等格式会读不到,建议使用mp3
let path = Bundle.main.path(forResource: "bongo0", ofType: "mp3")

//将字符串路径转换为网址路径
let soundUrl = URL(fileURLWithPath: path!)

AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
      
//对于静音按键,下拉菜单音等较短的声音,可以使用系统音频服务来播放
AudioServicesPlaySystemSound(soundId)