我是靠谱客的博主 刻苦乌冬面,这篇文章主要介绍IOS 播放系统提示音使用总结(AudioToolbox),现在分享给大家,希望可以做个参考。

IOS 播放系统提示音使用总结(AudioToolbox)

开发过程中需要用到苹果自带的系统提示音,下面我总结了一下关于系统提示音播放的方法

第一步首先得导入AudioToolbox框架

复制代码
1
#import <AudioToolbox/AudioToolbox.h>

播放系统自带的提示声

播放系统自带的提示声很简单,只需要两行代码就能搞定了:

复制代码
1
2
3
4
//定义一个SystemSoundID SystemSoundID soundID = 1000;//具体参数详情下面贴出来 //播放声音 AudioServicesPlaySystemSound(soundID);

关于SystemSoundID的相关参数介绍和系统所有的铃声的介绍

播放自定义的提示声,既有声音也带振动

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (void)playNotifySound { //获取路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"]; //定义一个SystemSoundID SystemSoundID soundID; //判断路径是否存在 if (path) { //创建一个音频文件的播放系统声音服务器 OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID); //判断是否有错误 if (error != kAudioServicesNoError) { NSLog(@"%d",(int)error); } } //播放声音和振动 AudioServicesPlayAlertSoundWithCompletion(soundID, ^{ //播放成功回调 }); }

只有振动没有声音

复制代码
1
2
//手机只振动没声音 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

只有声音不带振动

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//必须得是自定义的声音,经过测试系统的声音好像都带振动 - (void)playNotifySound { //获取路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"]; //定义一个带振动的SystemSoundID SystemSoundID soundID = 1000; //判断路径是否存在 if (path) { //创建一个音频文件的播放系统声音服务器 OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID); //判断是否有错误 if (error != kAudioServicesNoError) { NSLog(@"%d",(int)error); } } //只播放声音,没振动 AudioServicesPlaySystemSound(soundID); }

上面是我关于提示声使用的一些技巧,希望大家能学到东西,如果有不足希望大家给予补充,谢谢阅读!

最后

以上就是刻苦乌冬面最近收集整理的关于IOS 播放系统提示音使用总结(AudioToolbox)的全部内容,更多相关IOS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部