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