iOS音频—SystemSound
vibrate
使用如下的方法:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
使用了一个名为kSystemSoundID_Vibrate
的参数
需要导入#import <AudioToolbox/AudioServices.h>
测试了下,如果设置->声音->响铃模式振动or静音模式振动 为打开,则振动不会生效
AudioServicesPlayAlertSound方法有如下的说明:
iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration. However, the device does not vibrate if your app’s audio session is configured with the AVAudioSessionCategoryPlayAndRecord or AVAudioSessionCategoryRecord audio session category. This ensures that vibration doesn’t interfere with audio recording. For an explanation of audio session categories, see Categories Express Audio Roles.
如果audio session配置为AVAudioSessionCategoryPlayAndRecord 或者 AVAudioSessionCategoryRecord,则没有振动
UIFeedbackGenerator
iOS10中引入了触觉反馈UIFeedbackGenerator,它是个抽象类,不要继承或者直接使用这个类,要使用其具体类:
可参考如下的文档:
播放系统音效
参考:
播放系统音效需要导入AudioToolBox.framework
,使用的方法:
void AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID);
简单的例子:
SystemSoundID customSoundId = 1016;
AudioServicesPlaySystemSound(customSoundId);
其中的SystemSoundID,可参考http://iphonedevwiki.net/index.php/AudioServices
播放系统音效有如下的限制:
声音在当前系统音频卷中播放,没有编程音量控制可用
声音会立即播放
循环和立体定位是不可用的
每次只能播放一个声音,不支持同时播放多个声音
声音在设备扬声器上播放,不使用音频路由
也可以播放自定义的声音,Audio Tutorial for iOS: Playing Audio Programatically [2014 Edition],但有如下的限制:
- It only supports audio data formats linear PCM or IMA4.
- It only supports audio file formats CAF, AIF, or WAV.
- The sounds must be 30 seconds or less in length
- The sound plays at once.
- Only one sound can play at a time
- And more – see the Multimedia Programming Guide.
使用例子如下:
NSURL *systemSoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"]];
// 创建ID
SystemSoundID systemSoundID;
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(systemSoundURL), &systemSoundID);
// 注册callBack
AudioServicesAddSystemSoundCompletion(systemSoundID, nil, nil, soundFinishPlaying, nil);
// 播放系统声音
AudioServicesPlaySystemSound(systemSoundID);