您的位置:首页 > 移动开发 > IOS开发

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

2015-10-15 11:34 288 查看
一、今天项目中涉及了设置这快的声音震动和响铃,搞的头大,以前搞过,只是简单的调用系统的方法就可以实现,但是现在的公司要求,震动是震动,响铃是响铃,我看了微信,微信也是的分开的,做的很好,但是我就纳闷了,这要怎搞,网上查阅了好多方法,都是下面的代码。但是这样满足不了我的项目需求,我就纳闷的很,我设置了声音和震动,为什么在声音响起的时候,他会调用震动,这点让我很不解,于是网上查了好多。。。。网上代码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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: