ios 设置声音和震动,单独控制


一、今天项目中涉及了设置这快的声音震动和响铃,搞的头大,以前搞过,只是简单的调用系统的方法就可以实现,但是现在的公司要求,震动是震动,响铃是响铃,我看了微信,微信也是的分开的,做的很好,但是我就纳闷了,这要怎搞,网上查阅了好多方法,都是下面的代码。但是这样满足不了我的项目需求,我就纳闷的很,我设置了声音和震动,为什么在声音响起的时候,他会调用震动,这点让我很不解,于是网上查了好多。。。。网上代码90%都是这样写的

 

 1         //在线单聊
 2         if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isMessage_notf"] isEqualToString:@"0"]) {
 3             
 4         }else if([[[NSUserDefaults standardUserDefaults] objectForKey:@"isMessage_notf"] isEqualToString:@"1"] && [[[NSUserDefaults standardUserDefaults] objectForKey:@"isVoice_Set"] isEqualToString:@"1"]){
 5             
 6             AudioServicesPlaySystemSound (1007);//声音
 7             
 8         }else if([[[NSUserDefaults standardUserDefaults] objectForKey:@"isMessage_notf"] isEqualToString:@"1"] && [[[NSUserDefaults standardUserDefaults] objectForKey:@"isVibration_Set"] isEqualToString:@"1"])
 9         {
10             AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);//震动
11         }

当然这是我项目中我的代码咯,不过我也是网上看到的,直接拉下来用的,果不其然,这样写上去了,被测试部门,给打回来了,说震动关闭了,怎么还有震动,当时我抓狂啊,我也不知道为什么啊,我以前就是这样写就好 了的,哎,所以,就开始上网查,百度,谷歌,stockoverflow,等等一些网站看,都没用找到,最后在博客园看到了,然后就按照他说的来写了,他写的是一个单例,然后再震动的时候调用震动的方法,响铃的时候调用响铃的方法,所以我就写了一个单例来调用,果然解决了我的项目需求。。。

 1 在 LxxPlaySound.h 中的代码  2 
 3 #import <UIKit/UIKit.h>
 5 #import <AudioToolbox/AudioToolbox.h>
 6 
 9 @interface LxxPlaySound : NSObject 11 { 13  SystemSoundID soundID; 15 } 16 
17  
18 
19 /** 20 
21  * @brief 为播放震动效果初始化
25  * @return self 27  */
28 
29 -(id)initForPlayingVibrate; 33 /** 35  * @brief 为播放系统音效初始化(无需提供音频文件) 37  * 39  * @param resourceName 系统音效名称 41  * @param type 系统音效类型
45  * @return self 47  */
48 
49 -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
53 /**
55  * @brief 为播放特定的音频文件初始化(需提供音频文件)
59  * @param filename 音频文件名(加在工程中)
63  * @return self
65  */
66 
67 -(id)initForPlayingSoundEffectWith:(NSString *)filename;
71 /** 73  * @brief 播放音效 75  */
76 
77 -(void)play;
81 @end

 

 1 在 LxxPlaySound.m中的代码  2 
 3 #import "LxxPlaySound.h"
 7 @implementation LxxPlaySound
 11 -(id)initForPlayingVibrate  13 {  14 
 15     self = [super init];  17     if (self) {  19         soundID = kSystemSoundID_Vibrate;  21  }  23     return self;  25 }  26 
 27  
 28 
 29 -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type  31 {  32 
 33     self = [super init];  34 
 35     if (self) {  36 
 37         NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];  38 
 39         if (path) {  40 
 41  SystemSoundID theSoundID;  42 
 43             OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);  44 
 45             if (error == kAudioServicesNoError) {  46 
 47                 soundID = theSoundID;  48 
 49             }else {  50 
 51                 NSLog(@"Failed to create sound ");  52 
 53  }  54 
 55  }
 59  }  60 
 61     return self;  62 
 63 }  64 
 67 -(id)initForPlayingSoundEffectWith:(NSString *)filename
 69 {  70 
 71     self = [super init];  72 
 73     if (self) {  74 
 75         NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];  76 
 77         if (fileURL != nil)
 79  {  80 
 81  SystemSoundID theSoundID;
 83             OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);  84 
 85             if (error == kAudioServicesNoError){  86 
 87                 soundID = theSoundID;  88 
 89             }else {  90 
 91                 NSLog(@"Failed to create sound ");  92 
 93  }
 95  }
 97  }  98 
 99     return self; 100 
101 } 102 
103  
104 
105 -(void)play
107 {
109  AudioServicesPlaySystemSound(soundID);
111 } 112 
113  
114 
115 -(void)dealloc
117 { 118 
119  AudioServicesDisposeSystemSoundID(soundID);
121 } 122 
123 @end

然后在你的震动和声音那里开始来调用他。。只需引入他的头文件,就万事俱备,开始你的设置吧。。。你的设置就可以任你调用,想震动震动,想开铃声开铃声。。。。

如下代码演示调用震动,当然如果震动和声音一起就要换成soundID换位1007 。。还有如果你要声音的话,就用网上

// 震动 首先要引入头文件

  LxxPlaySound *playSound =[[LxxPlaySound alloc]initForPlayingVibrate]; 
 [playSound play];

 

希望可以帮到一些像我这样的苦逼的码农,如有不清楚的尽情来找我,互相学习,共同努力!!day day up!!!

可以加qq群:370624831 

 

最近好多人问我这个问题,我写了一个demo,你们可以去 CSDN 下载,下载地址:iOS单独控制 声音 和 震动

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM