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>
 4 
 5 #import <AudioToolbox/AudioToolbox.h>
 6 
 7  
 8 
 9 @interface LxxPlaySound : NSObject
10 
11 {
12 
13     SystemSoundID soundID;
14 
15 }
16 
17  
18 
19 /**
20 
21  *  @brief  爲播放震動效果初始化
22 
23  *
24 
25  *  @return self
26 
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 self
46 
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 self
64 
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)play
106 
107 {
108 
109     AudioServicesPlaySystemSound(soundID);
110 
111 }
112 
113  
114 
115 -(void)dealloc
116 
117 {
118 
119     AudioServicesDisposeSystemSoundID(soundID);
120 
121 }
122 
123 @end
複製代碼

然後在你的震動和聲音那裏開始來調用他。。只需引入他的頭文件,就萬事俱備,開始你的設置吧。。。你的設置就可以任你調用,想震動震動,想開鈴聲開鈴聲。。。。

如下代碼演示調用震動

// 震動 首先要引入頭文件

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

 

發佈了22 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章