主要调用AudioServicesPlaySystemSoundWithVibration这个private
api,传入一个含有时间和强度的dictionary即可。
注意要引入AudioToolbox.framework。在iOS6,iOS7上测试通过。
private api有风险,注意不要在上架app中使用。
1 NSMutableDictionary* dict = [NSMutableDictionary dictionary]; 2 NSMutableArray* arr = [NSMutableArray array ]; 3 4 [arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms 5 [arr addObject:[NSNumber numberWithInt:2000]]; 6 7 8 [dict setObject:arr forKey:@"VibePattern"]; 9 [dict setObject:[NSNumber numberWithFloat:0.3] forKey:@"Intensity"]; 10 11 AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff);
写了一个封装demo
.h
1 // 2 // YANGZShaking.h 3 // shaking 4 // 5 // Created by mmm on 14-1-11. 6 // Copyright (c) 2014年 yangz. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface YANGZVibePattern : NSObject 12 + (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe; 13 @property (nonatomic) bool isV; 14 @property (nonatomic) float intensity; 15 @property (nonatomic) NSUInteger time; 16 17 @end 18 19 @interface YANGZShaking : NSObject 20 21 22 - (YANGZShaking *)initWithPattern:(NSArray *)patterns; 23 - (YANGZShaking *)initWithFile:(NSString *)file; 24 - (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause; 25 - (void)play; 26 - (void)stop; 27 28 @end
.m
1 // 2 // YANGZShaking.m 3 // shaking 4 // 5 // Created by mmm on 14-1-11. 6 // Copyright (c) 2014年 yangz. All rights reserved. 7 // 8 9 10 #import "YANGZShaking.h" 11 #import <AudioToolbox/AudioToolbox.h> 12 13 14 @implementation YANGZVibePattern 15 + (YANGZVibePattern *)PatternWithIntensity:(float)intensity time:(NSUInteger)time isVibe:(BOOL)isVibe{ 16 YANGZVibePattern *pattern = [YANGZVibePattern new]; 17 pattern.isV = isVibe; 18 pattern.intensity = intensity; 19 pattern.time = time; 20 return pattern; 21 }; 22 @end 23 24 25 @interface YANGZShaking(){ 26 NSArray *_patterns; 27 NSMutableDictionary *_buff; 28 bool _isRepeat; 29 } 30 @end 31 32 33 @implementation YANGZShaking 34 35 36 - (YANGZShaking *)initWithPattern:(NSArray *)patterns{ 37 if ([super init]) { 38 _patterns = [[NSMutableArray alloc] initWithArray:patterns copyItems:YES]; 39 _buff=nil; 40 _isRepeat=NO; 41 } 42 return self; 43 } 44 45 46 - (YANGZShaking *)initWithFile:(NSString *)file{ 47 if ([super init]) { 48 _patterns = [NSMutableArray arrayWithContentsOfFile:file]; 49 _buff=nil; 50 _isRepeat=NO; 51 } 52 return self; 53 } 54 - (YANGZShaking *)initWithLoop:(NSUInteger)vibe pause:(NSUInteger)pause{ 55 if ([super init]) { 56 YANGZVibePattern *vibePattern = [YANGZVibePattern PatternWithIntensity:0.3 time:vibe isVibe:YES]; 57 YANGZVibePattern *pausePattern = [YANGZVibePattern PatternWithIntensity:0.9 time:pause isVibe:NO]; 58 59 _patterns = @[vibePattern,pausePattern]; 60 _buff=nil; 61 _isRepeat=YES; 62 } 63 return self; 64 65 66 } 67 - (void)play{ 68 [NSThread detachNewThreadSelector:@selector(loop) toTarget:self withObject:nil]; 69 } 70 - (void)stop{ 71 72 } 73 74 75 - (void)loop{ 76 NSUInteger i=0; 77 while (YES) { 78 NSUInteger pause = 0; 79 YANGZVibePattern *pattern = _patterns; 80 if (!pattern.isV) { 81 } 82 else{ 83 [self prepareBuffWithPattern:pattern]; 84 [self performSelectorOnMainThread:@selector(systemPlay) withObject:nil waitUntilDone:NO]; 85 } 86 [NSThread sleepForTimeInterval:pattern.time*1.0f/1000]; 87 i++; 88 if (i==[_patterns count] && !_isRepeat) { 89 break; 90 } 91 i=i%[_patterns count]; 92 } 93 } 94 - (void)prepareBuffWithPattern:(YANGZVibePattern *)pattern{ 95 NSMutableDictionary* dict = [NSMutableDictionary dictionary]; 96 NSMutableArray* arr = [NSMutableArray array ]; 97 98 [arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms 99 [arr addObject:[NSNumber numberWithInt:pattern.time]]; 100 101 102 103 [dict setObject:arr forKey:@"VibePattern"]; 104 [dict setObject:[NSNumber numberWithFloat:pattern.intensity] forKey:@"Intensity"]; 105 _buff = dict; 106 } 107 - (void)systemPlay{ 108 NSLog(@"system play @%"); 109 110 AudioServicesPlaySystemSoundWithVibration(4095,nil,_buff); 111 } 112 @end