1,系统声音服务介绍:
系统声音服务提供了一个Api,用于播放不超过30秒的声音。它支持的文件格式有限,具体的说只有CAF、AIF和使用PCM或IMA/ADPCM数据的WAV文件。
但此函数没有提供操作声音和控制音量的功能,因此如果是要为多媒体或游戏创建专门声音,就不要使用系统声音服务。
2,系统声音服务支持如下三种类型:
(1)声音:立刻播放一个简单的声音文件。如果手机静音,则用户什么也听不见。
(2)提醒:播放一个声音文件,如果手机设为静音或震动,则通过震动提醒用户。
(3)震动:震动手机,而不考虑其他设置。
3,使用样例(首先类中要引入AudioToolbox)
|
1
|
import AudioToolbox
|
(1)声音播放
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@IBAction func systemSound(sender: AnyObject) {
//建立的SystemSoundID对象
var soundID:SystemSoundID = 0
//获取声音地址
var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")
//地址转换
var baseURL = NSURL(fileURLWithPath: path!)
//赋值
AudioServicesCreateSystemSoundID(baseURL, &soundID)
//播放声音
AudioServicesPlaySystemSound(soundID)
} |
(2)提醒
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@IBAction func systemAlert(sender: AnyObject) {
//建立的SystemSoundID对象
var soundID:SystemSoundID = 0
//获取声音地址
var path = NSBundle.mainBundle().pathForResource("msg", ofType: "wav")
//地址转换
var baseURL = NSURL(fileURLWithPath: path!)
//赋值
AudioServicesCreateSystemSoundID(baseURL, &soundID)
//提醒(同上面唯一的一个区别)
AudioServicesPlayAlertSound(soundID)
} |
(3)振动
|
1
2
3
4
5
6
|
@IBAction func systemVibration(sender: AnyObject) {
//建立的SystemSoundID对象
var soundID = SystemSoundID(kSystemSoundID_Vibrate)
//振动
AudioServicesPlaySystemSound(soundID)
} |