最近做的項(xiàng)目里剛好用到提示音,所以就把自己寫了個(gè)簡單的 demo 跟大家分享一下:
demo GitHub地址
兩種提示方式:
1.調(diào)用系統(tǒng)的提示音:這種方式很簡單,直接指定soundID,即可
-- [系統(tǒng)提示音soundID 官方說明:點(diǎn)擊這里可以查看soundID]
(http://iphonedevwiki.net/index.php/AudioServices)
soundID范圍:1000 to 2000,比如,1007就是蘋果默認(rèn)的三全音提示,其他的大家可以自己意義試聽(哈哈,太多了)
--AudioServicesPlaySystemSound(soundID) **
2.使用自定義的提示音(時(shí)間必須小于30秒. caf 文件)
-- 這種方式需要讀取工程里的音頻文件(.caf)格式的,然后添加到系統(tǒng)提示音里面,得到 soundID ,然后調(diào)用AudioServicesPlaySystemSound(soundID) **即可
具體實(shí)現(xiàn):本 demo 提供最常見了三中方式:
1.聲音提示(我覺得這個(gè)應(yīng)該是用的最多了吧,誰也不愿意每次收到微信消息都振動(dòng)吧,??)AudioServicesPlaySystemSound(soundID) **
2.振動(dòng)提示(靜音狀態(tài))AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)**
3.聲音加振動(dòng)(感覺很少用到哈)** AudioServicesPlayAlertSound(soundID)**
iOS系統(tǒng)提供了AudioToolbox.framework 框架,需要導(dǎo)入<AudioToolbox/AudioToolbox.h>頭文件.
注意:
這個(gè)庫是伴隨系統(tǒng)聲音設(shè)置的(也就是說,如果系統(tǒng)為靜音或者是振動(dòng),提示也就是振動(dòng)或者沒有提示)
廢話不多說了,具體直接上代碼
調(diào)用方法:
AudioOnly = 1, // 聲音提示
VibrateOnly, // 振動(dòng)提示
AudioAndVibrate // 聲音&振動(dòng)
AudioServicesManager *manager = [AudioServicesManager sharedManager];
// 設(shè)置提示類型
manager.audioServicesType = AudioOnly;
// 觸發(fā)提示
[manager play];
AudioServicesManager.h文件
//
// AudioServicesManager.h
// 音效播放
//
// Created by lihaohao on 2017/5/12.
// Copyright ? 2017年 lihaohao. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger){
AudioOnly = 1, // 聲音提示
VibrateOnly, // 振動(dòng)提示
AudioAndVibrate // 聲音&振動(dòng)
}AudioServicesType;
@interface AudioServicesManager : NSObject
@property (nonatomic ,assign) AudioServicesType audioServicesType;
+ (instancetype)sharedManager;
- (void)play;
@end
AudioServicesManager.m文件
//
// AudioServicesManager.m
// 音效播放
//
// Created by lihaohao on 2017/5/12.
// Copyright ? 2017年 lihaohao. All rights reserved.
//
#import "AudioServicesManager.h"
#import <AudioToolbox/AudioToolbox.h>
NSString *const kFileUrl = @"sound.caf";
@interface AudioServicesManager()
@property (nonatomic ,assign) SystemSoundID soundID;
@end
@implementation AudioServicesManager
+ (instancetype)sharedManager{
static AudioServicesManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[AudioServicesManager alloc]init];
});
return manager;
}
- (instancetype)init{
self = [super init];
if (self) {
NSString *audioFile=[[NSBundle mainBundle] pathForResource:kFileUrl ofType:nil];
NSURL *fileUrl=[NSURL fileURLWithPath:audioFile];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &_soundID);
}
return self;
}
#pragma mark -
#pragma mark - play
-(void)play{
NSLog(@"%d",_soundID);
switch (_audioServicesType) {
case AudioOnly:
[self audioOnly];
break;
case VibrateOnly:
[self vibrateOnly];
break;
case AudioAndVibrate:
[self audioAndVibrate];
break;
default:
break;
}
}
- (void)audioOnly{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
AudioServicesPlaySystemSound(_soundID);
#else
AudioServicesPlaySystemSoundWithCompletion(_soundID, nil);
#endif
}
- (void)vibrateOnly{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
#else
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
#endif
}
- (void)audioAndVibrate{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
AudioServicesPlayAlertSound(_soundID);
#else
AudioServicesPlayAlertSoundWithCompletion(_soundID,nil);
#endif
}
#pragma mark -
#pragma mark - dealloc
- (void)dealloc{
AudioServicesRemoveSystemSoundCompletion(_soundID);
AudioServicesDisposeSystemSoundID(_soundID);
}
@end
尾巴:
測試發(fā)現(xiàn)在調(diào)用系統(tǒng)的三全音的時(shí)候,在 iOS 10.3.1下,只要設(shè)置聲音就會有振動(dòng),不管調(diào)用AudioServicesPlayAlertSound,還是AudioServicesPlaySystemSound在系統(tǒng)設(shè)置為聲音加振動(dòng)的情況下,由于我們需要單獨(dú)控制聲音和振動(dòng),用自己定義的聲音文件就可以單獨(dú)控制聲音和振動(dòng)提醒.