一. 系统音效(短音效播放) 引用
AudioToolbox framework
使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。
1、 播放的时间不能超过30秒
2、数据必须是 PCM或者IMA4流格式
3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff)
声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。
二、具体使用
NSURL *audioPath = [[NSBundle mainBundle] URLForResource:@"in" withExtension:@"caf"];
// 创建系统声音,同时返回一个ID
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(audioPath), &soundID);
// Register the sound completion callback.
AudioServicesAddSystemSoundCompletion(soundID,
NULL, // uses the main run loop
NULL, // uses kCFRunLoopDefaultMode
EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
NULL // for user data, but we don't need to do that in this case, so we just pass NULL
);
AudioServicesPlaySystemSound(soundID);
震动
// 震动
- (void)playVibration
{
// Register the sound completion callback.
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,
NULL, // uses the main run loop
NULL, // uses kCFRunLoopDefaultMode
EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
NULL // for user data, but we don't need to do that in this case, so we just pass NULL
);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}