標籤:
一、今天項目中涉及了設定這快的聲音震動和響鈴,搞的頭大,以前搞過,只是簡單的調用系統的方法就可以實現,但是現在的公司要求,震動是震動,響鈴是響鈴,我看了,也是的分開的,做的很好,但是我就納悶了,這要怎搞,網上查閱了好多方法,都是下面的代碼。但是這樣滿足不了我的項目需求,我就納悶的很,我設定了聲音和震動,為什麼在聲音響起的時候,他會調用震動,這點讓我很不解,於是網上查了好多。。。。網上代碼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 }
當然這是我項目中My Code咯,不過我也是網上看到的,直接拉下來用的,果不其然,這樣寫上去了,被測試部門,給打回來了,說震動關閉了,怎麼還有震動,當時我抓狂啊,我也不知道為什麼啊,我以前就是這樣寫就好 了的,哎,所以,就開始上網查,百度,Google,stockoverflow,等等一些網站看,都沒用找到,最後在部落格園看到了,然後就按照他說的來寫了,他寫的是一個單例,然後再震動的時候調用震動的方法,響鈴的時候調用響鈴的方法,所以我就寫了一個單例來調用,果然解決了我的項目需求。。。
1 在 LxxPlaySound.h 中的代碼 2 3 #import <UIKit/UIKit.h> 4 5 #import <AudioToolbox/AudioToolbox.h> 6 7 8 9 @interface LxxPlaySound : NSObject10 11 {12 13 SystemSoundID soundID;14 15 }16 17 18 19 /**20 21 * @brief 為播放震動效果初始化22 23 *24 25 * @return self26 27 */28 29 -(id)initForPlayingVibrate;30 31 32 33 /**34 35 * @brief 為播放系統音效初始化(無需提供音頻檔案)36 37 *38 39 * @param resourceName 系統音效名稱40 41 * @param type 系統音效類型42 43 *44 45 * @return self46 47 */48 49 -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;50 51 52 53 /**54 55 * @brief 為播放特定的音頻檔案初始化(需提供音頻檔案)56 57 *58 59 * @param filename 音頻檔案名稱(加在工程中)60 61 *62 63 * @return self64 65 */66 67 -(id)initForPlayingSoundEffectWith:(NSString *)filename;68 69 70 71 /**72 73 * @brief 播放音效74 75 */76 77 -(void)play;78 79 80 81 @end
1 在 LxxPlaySound.m中的代碼 2 3 #import "LxxPlaySound.h" 4 5 6 7 @implementation LxxPlaySound 8 9 10 11 -(id)initForPlayingVibrate 12 13 { 14 15 self = [super init]; 16 17 if (self) { 18 19 soundID = kSystemSoundID_Vibrate; 20 21 } 22 23 return self; 24 25 } 26 27 28 29 -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type 30 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 } 56 57 58 59 } 60 61 return self; 62 63 } 64 65 66 67 -(id)initForPlayingSoundEffectWith:(NSString *)filename 68 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) 78 79 { 80 81 SystemSoundID theSoundID; 82 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 } 94 95 } 96 97 } 98 99 return self;100 101 }102 103 104 105 -(void)play106 107 {108 109 AudioServicesPlaySystemSound(soundID);110 111 }112 113 114 115 -(void)dealloc116 117 {118 119 AudioServicesDisposeSystemSoundID(soundID);120 121 }122 123 @end
然後在你的震動和聲音那裡開始來調用他。。只需引入他的標頭檔,就萬事俱備,開始你的設定吧。。。你的設定就可以任你調用,想震動震動,想開鈴聲開鈴聲。。。。
如下代碼示範調用震動
// 震動 首先要引入標頭檔 LxxPlaySound *playSound =[[LxxPlaySound alloc]initForPlayingVibrate];
[playSound play];
這是我苦逼了二天才解決的問題,只因我太菜,我大哥一直在幫我,謝謝他 @平淡的我 。。。
希望可以幫到一些像我這樣的苦逼的碼農,如有不清楚的盡情來找我,互相學習,共同努力!!day day up!!!
ios 設定聲音和震動,單獨控制