播放系統(tǒng)音效工具類

ps:功能——隨機(jī)播放系統(tǒng)音效,帶震動(dòng)效果,并提供彈出框確認(rèn),目的是給應(yīng)用提供找手機(jī)發(fā)出聲音的功能

1.便捷創(chuàng)建單例

//  簡(jiǎn)便創(chuàng)建單例的宏

#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)具體實(shí)現(xiàn)

#import "ALAudioTool.h"
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIKit.h>

@interface ALAudioTool()
@property (nonatomic,assign) BOOL isNotRepeat;/**< 判斷是否重復(fù)播放 */
@property (nonatomic,strong) NSArray *audioSourceArray;/**< 聲源數(shù)組 */
@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.發(fā)送已收到提示的反饋命令  表示已經(jīng)找到手機(jī)
        [[ALCommandTool shareTool] reactionFindMobilePhone:^(id responseObj) {
            NSLog(@"手機(jī)找到了");
        }];
    }
}


- (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;
}


//獲取一個(gè)隨機(jī)整數(shù),范圍在[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);  // 播放系統(tǒng)聲音 這里的sound是我自定義的,不要copy哈,沒有的
}

- (void)playSystemAudio{
    //1.收到手機(jī)響應(yīng)結(jié)果,播放一段音頻
    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];

4.參考

http://www.cnblogs.com/Xfsrn/p/4997476.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容