当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 播放系统提示音使用总结(AudioToolbox)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

艾达王和里昂那一晚,魔幻三兄弟,廉价孤儿药断供

ios 播放系统提示音使用总结(audiotoolbox)

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

第一步首先得导入audiotoolbox框架

#import <audiotoolbox/audiotoolbox.h>

播放系统自带的提示声

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

//定义一个systemsoundid
 systemsoundid soundid = 1000;//具体参数详情下面贴出来
 //播放声音
 audioservicesplaysystemsound(soundid);

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

- (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, ^{
  //播放成功回调
 });
}

只有振动没有声音

 //手机只振动没声音
 audioservicesplaysystemsound(ksystemsoundid_vibrate);

只有声音不带振动

//必须得是自定义的声音,经过测试系统的声音好像都带振动
- (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);
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网