iOS音频---SystemSound

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值