在项目中使用AudioServicesPlaySystemSound 这个接口来进行声音和震动的播放, 当然需要在工程中加入AudioToolBox.framework

我们可以写一个文件来封装声音和震动的各项功能,调用时使用单例比较方便。

我写的文件messageSound

在messageSound.h文件中

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

#import <AudioToolbox/AudioToolbox.h>

@interface messageSound : NSObject

{

SystemSoundID soundID;

}

@property (nonatomic,assign) BOOL isON;

//分别为震动和声音设置的系统单列

+ (id) sharedInstanceForVibrate;

+ (id) sharedInstanceForSound;

/**

*@brief 为震动效果初始化

*

*@return self

*/

-(id)initForPlayingVibrate;

/**

*  @brief  为播放系统音效初始化(无需提供音频文件)

*

*  @param resourceName 系统音效名称

*  @param type 系统音效类型

*

*  @return self

*/

-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;

/*

* @brief 为播放特定的音频文件初始化 (需提供音频文件)

*

*@param filename 音频文件名(加在工程中)

*

*@return self

*/

-(id)initForPlayingSoundEffectWith:(NSString *)filename;

/*

* @brief 播放音效

*/

-(void)play;

-(void)cancleSound;

@end

在message.m文件中代码如下

#import "messageSound.h"

@implementation messageSound

static messageSound *_sharedInstance;

static messageSound *_sharedInstanceForSound;

+(id)sharedInstanceForVibrate

{

@synchronized ([messageSound class]) {

if (_sharedInstance == nil) {

_sharedInstance = [[messageSound alloc] initForPlayingVibrate];

}

}

return _sharedInstance;

}

+ (id) sharedInstanceForSound

{

@synchronized ([messageSound class]) {

if (_sharedInstanceForSound == nil) {

_sharedInstanceForSound = [[messageSound alloc] initForPlayingSystemSoundEffectWith:@"sms-received2" ofType:@"caf"];

}

}

return _sharedInstanceForSound;

}

-(id)initForPlayingVibrate

{

self=[super init];

if(self){

soundID=kSystemSoundID_Vibrate;

}

return self;

}

-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type

{

self=[super init];

if(self){

//        NSString *path=[[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];

NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",resourceName,type];

if(path){

SystemSoundID theSoundID;

OSStatus error =AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&theSoundID);

if(error == kAudioServicesNoError){

soundID=theSoundID;

}else{

NSLog(@"Failed to create sound");

}

}

}

return  self;

}

-(id)initForPlayingSoundEffectWith:(NSString *)filename

{

self=[super init];

if(self){

NSURL *fileURL=[[NSBundle mainBundle]URLForResource:filename withExtension:nil];

if(fileURL!=nil){

SystemSoundID theSoundID;

OSStatus error=AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);

if(error ==kAudioServicesNoError){

soundID=theSoundID;

}else{

NSLog(@"Failed to create sound");

}

}

}

return self;

}

-(void)play

{

AudioServicesPlaySystemSound(soundID);

}

-(void)cancleSound

{

_sharedInstance=nil;

//AudioServicesRemoveSystemSoundCompletion(soundID);

}

-(void)dealloc

{

  AudioServicesDisposeSystemSoundID(soundID);

}

@end

至于怎么使用,我是这么用的,在appdelegate.m文件中开启系统音效设置的单例

//设置系统音效

[messageSound sharedInstanceForSound];

//设置系统震动

[messageSound sharedInstanceForVibrate];

在app设置中,把声音和震动着两个开关的状态保存在本地设置文件中,在播放系统音效的地方,读取本地设置文件的开关状态,根据状态来判断播放声音还是播放震动或者二者兼有,代码如下。(音效文件我是通过yyCache保存在本地的)

messageSound *ms=[messageSound sharedInstanceForVibrate];

messageSound *ms1=[messageSound sharedInstanceForSound];

NSDictionary *localDict;

if([[YYCache sharedInstance]containsObjectForKey:[NSString stringWithFormat:@"%@%@",[AccountTools sharedAccountTools].currentAccount.uid,@"messageSoundSetting"]]==YES)

{

localDict=[[YYCache sharedInstance] objectForKey:[NSString stringWithFormat:@"%@%@",[AccountTools sharedAccountTools].currentAccount.uid,@"messageSoundSetting"]];

if([[localDict objectForKey:@"Sound"] intValue]==1)

{

[ms1 play];

}

if([[localDict objectForKey:@"Vibrate"] intValue]==1)

{

[ms play];

}

}

这样就实现了,微信设置中那种单独设置声音和震动的效果。
iOS菜逼的第一篇技术博客,谢谢,其中部分代码是参考别人的代码仿写的。

ios中设置app音效音效和震动相关推荐

  1. iPhone App创建与审核步骤二:如何在developer.apple.com网站中设置App预览和截屏以完成App上架

    iPhone App创建与审核步骤二:如何在developer.apple.com网站中设置App预览和截屏以完成App上架,根据图标规范RAD Studio 10.4 for delphi XE 或 ...

  2. 在 iPhone 中设置 APP 的使用时间!合理使用手机时间

    假期在家的孩子玩手机时间过长,相信许多家长可能都会对此感到很头大!下面和大家分享一个 iPhone 的使用小技巧,可以限制小朋友玩游戏或是看影片的时间. 启用「引导使用模式」限制 App 使用时间 如 ...

  3. ios中设置URL Scheme及如何跳转到其他APP中

    URL Scheme的作用 我们都知道苹果手机中的APP都有一个沙盒,APP就是一个信息孤岛,相互是不可以进行通信的.但是iOS的APP可以注册自己的URL Scheme,URL Scheme是为方便 ...

  4. 浅谈iOS中关于app的优化

    目录 我要给出的建议将分为三个不同的等级: 入门级. 中级和进阶级: 入门级(这是些你一定会经常用在你app开发中的建议) 1. 用ARC管理内存 2. 在正确的地方使用reuseIdentifier ...

  5. iOS中设置导航栏标题( titleView)的字体颜色和大小

    在iOS中,经常会对一些导航栏titleView进行自定义,首先介绍一下对navgationBar 上的title设置的三种方法: <1> self.title = @"我是ti ...

  6. ios中在app中安装ipa的方法

    转自http://www.yonsm.net/post/553,http://since2006.com/blog/240/ios6-mobileinstallationinstall iOS 中要安 ...

  7. ios中在app应用内刷新小组件数据

    需求: 我们需要在app应用内刷新时间线,让桌面小组件加载最新的内容.即app内修改了共享数据后,需要通知桌面小组件强制刷新,显示改变后的内容. 当某种情况影响到小组件的当前时间线时,您的 App 可 ...

  8. IOS中设置全局变量

    转:http://blog.csdn.net/totogogo/article/details/7355203 有几种方法 some developers recommend use singleto ...

  9. IOS中设置圆角图片

    2019独角兽企业重金招聘Python工程师标准>>> ##iOS设置圆角的三种方式 <hr/> 1 方法一 通过设置layer的属性 UIImageView *imag ...

最新文章

  1. Boost:自定义静态向量
  2. Linux执行命令unable to create new native thread问题
  3. CSS: 首字母字体变大时下划线不对齐的解决方法
  4. 【转】Epoll模型
  5. BAT面试进阶:最全Memcached面试30题含答案
  6. MATLAB设置x为0到10所有数,MATLAB教学_10数值微积分
  7. Linux学习笔记之系统路径和命令
  8. JSON jquery 与php 入门
  9. JavaScript(五)——错误处理
  10. php 横屏和竖屏,面试问题,视频横屏与竖屏的设计差异?我是这样回答的
  11. python爬虫豆瓣TOP250电影信息并写入数据库
  12. 40岁后学习编程是否太晚了?7点技巧让学习变得轻松有趣
  13. Debian添加开机启动项
  14. 机电信息杂志社《机电信息》杂志社机电信息杂志社2022年第24期目录
  15. 终于搞定Paypal了
  16. c语言排序算法插入法,C语言中冒泡法、选择法、插入法三种常见排序算法分析.doc...
  17. 缅怀钟扬教授,5分钟视频回顾伟大植物学家钟扬对人类的贡献
  18. cf: Ehab and Path-etic MEXs
  19. 网络安全 中间人攻击-DNS欺骗 使用ettercap
  20. GO、Java、C/C++、Python,这些编程语言谁才是王者?

热门文章

  1. 程序员如何防止脑疲劳:下午补充一些干果
  2. Android震动和自定义铃声
  3. IntelliJ IDEA自定义菜单(Menus)、任务栏(toolbars)详细教程(即Customize Menus and Toolbars...)
  4. java mocked,JMockit 中被 Mocked 的对象属性及方法的默认值
  5. 快速理解深度信念网络
  6. 万维钢:发现效率的眼睛
  7. selenium下拉列表定位之 select+option 的定位
  8. 2021年全球与中国水上巡航行业市场规模及发展前景分析
  9. 容器部署在物理机还是虚拟机上?
  10. 用python制作简单的可视化地图