ps:功能——随机播放系统音效,带震动效果,并提供弹出框确认,目的是给应用提供找手机发出声音的功能
1.便捷创建单例
// 简便创建单例的宏
#ifndef DLInterviewTest_DLSingleton_h
#define DLInterviewTest_DLSingleton_h
// .h文件
#define DLSingletonH(name) + (instancetype)shared##name;
// .m文件
#define DLSingletonM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
#endif
2.核心代码
1)头文件
#import <Foundation/Foundation.h>
@interface ALAudioTool : NSObject
DLSingletonH(ALAudioTool)
- (void)showMyPhone;
@end
2)具体实现
#import "ALAudioTool.h"
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIKit.h>
@interface ALAudioTool()
@property (nonatomic,assign) BOOL isNotRepeat;/**< 判断是否重复播放 */
@property (nonatomic,strong) NSArray *audioSourceArray;/**< 声源数组 */
@property (nonatomic,copy) NSString *audioID;/**< 声源ID */
@end
@implementation ALAudioTool
DLSingletonM(ALAudioTool)
- (void)setIsNotRepeat:(BOOL)isNotRepeat{
_isNotRepeat = isNotRepeat;
if (isNotRepeat) {
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
SystemSoundID soundID = [self.audioID intValue];
[self stopAlertSoundWithSoundID:soundID];
//2.发送已收到提示的反馈命令 表示已经找到手机
[[ALCommandTool shareTool] reactionFindMobilePhone:^(id responseObj) {
NSLog(@"手机找到了");
}];
}
}
- (NSArray *)audioSourceArray{
if (!_audioSourceArray) {
_audioSourceArray = [NSArray arrayWithObjects:@"1000",@"1001",@"1002",@"1003"@"1004",@"1005",@"1006",@"1007",@"1008",@"1009",@"1010",@"1011",@"1012",@"1013",@"1014",@"1015",@"1016",@"1020",@"1021",@"1022",@"1023",@"1024",@"1025",@"1026",@"1027",@"1028",@"1029",@"1030",@"1031",@"1032",@"1033",@"1034",@"1035",@"1036",@"1050",@"1051",@"1052",@"1053",@"1054",@"1055",@"1056",@"1057",@"1070",@"1071",@"1072",@"1073",@"1074",@"1075",@"1100",@"1101",@"1102",@"1103",@"1104",@"1105",@"1106",@"1107",@"1108",@"1109",@"1110",@"1111",@"1112",@"1113",@"1114",@"1115",@"1116",@"1117",@"1118",@"1150",@"1151",@"1152",@"1153",@"1154",@"1200",@"1201",@"1202",@"1203",@"1204",@"1205",@"1206",@"1207",@"1208",@"1209",@"1210",@"1211",@"1254",@"1255",@"1256",@"1257",@"1258",@"1259",@"1300",@"1301",@"1302",@"1303",@"1304",@"1305",@"1306",@"1307",@"1308",@"1309",@"1310",@"1311",@"1312",@"1313",@"1314",@"1315",@"1320",@"1321",@"1322",@"1323",@"1324",@"1325",@"1326",@"1327",@"1328",@"1329",@"1330", nil];
}
return _audioSourceArray;
}
//获取一个随机整数,范围在[from,to],包括from,包括to
-(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to{
return (NSInteger)(from + (arc4random() % (to - from + 1)));
}
- (void)showMyPhone{
self.isNotRepeat = NO;
self.audioID = self.audioSourceArray[[self getRandomNumber:0 to:(self.audioSourceArray.count - 1)]];
[self addAlertView];
[self playSystemAudio];
}
void soundCompleteCallback(SystemSoundID sound,void * clientData) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(sound); // 播放系统声音 这里的sound是我自定义的,不要copy哈,没有的
}
- (void)playSystemAudio{
//1.收到手机响应结果,播放一段音频
if (!self.isNotRepeat) {
SystemSoundID soundID = [self.audioID intValue];
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(soundID);
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
}
-(void)stopAlertSoundWithSoundID:(SystemSoundID)sound {
AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
AudioServicesDisposeSystemSoundID(sound);
AudioServicesRemoveSystemSoundCompletion(sound);
}
-(void)addAlertView{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
UBTLocalizedString(@"phone_search_title") message:UBTLocalizedString(@"phone_search_subtitle") delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView show];
}
#pragma mark - Alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
NSLog(@"Cancel button clicked");
break;
case 1:
NSLog(@"OK button clicked");
//停止播放
self.isNotRepeat = YES;
break;
default:
break;
}
}
@end
3.使用方式
[[ALAudioTool sharedALAudioTool] showMyPhone];