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

导入UIKit

//导入音频工具箱框架,这样就可以使用系统声音服务了

导入AudioToolbox

//开始前需要右键点击项目名称,添加一个声音文件(在测试时添加了acc文件无效,错误“path = nil”)

class ViewController:UIViewController {

override func viewDidLoad(){

super.viewDidLoad()

//在加载视图后进行任何其他设置,通常是从笔尖。

//声明一个系统声音标示类型声音变量

var _soundId:SystemSoundID = 0

//获取沙箱目录中文件所在的路径

let path = Bundle.main.path(forResource:“test”,ofType:“mp3”)

//将字符串路径转换为网址路径

让soundUrl = URL(fileURLWithPath:path!)

AudioServicesCreateSystemSoundID(soundUrl as CFURL,&_soundId)

//对于静音按键,下拉菜单音等较短的声音,可以使用系统音频服务来播放

AudioServicesPlaySystemSound(_soundId)

}

override func didReceiveMemoryWarning(){

super.didReceiveMemoryWarning()

//处理可以重新创建的任何资源。

}

}

猜你喜欢

转载自blog.csdn.net/weixin_41735943/article/details/81199254