語音播報功能的實現(xiàn)必須是推送加語音合成,選擇的推送是極光推送,本文最終實現(xiàn)的效果即使APP被殺死也可以進行語音播報
配置推送證書,極光文檔里面有,把極光推送集成進去就不說了
語音合成我用的是系統(tǒng)的方法,不過語音死板不好聽,但是使用很簡單,3行代碼就可以,建議使用其他的SDK
AVSpeechUtterance*utterance = [AVSpeechUtterancespeechUtteranceWithString:@"成功集成語音播報"];
AVSpeechSynthesizer*synth = [[AVSpeechSynthesizeralloc] init];
[synth speakUtterance:utterance];
在收到通知的時候使用上面的3行代碼就可以進行語音播報,但是只限于APP前臺運行,當后臺運行的時候語音播報便不可以了,此時需要加入下面代碼讓語音播報可以在后臺運行,但是殺死的情況下不能播報,殺死重新啟動返回后臺也不可以播報. 我是在AppDelegate里面寫入這個方法的
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions;
把下面的代碼寫入進去
NSError*error =NULL;
AVAudioSession*session = [AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryPlaybackerror:&error];
if(error) {
// Do some error handling
}
[session setActive:YESerror:&error];
if(error) {
// Do some error handling
}
// 讓app支持接受遠程控制事件
[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];
讓語音播報在后臺也可以進行的話就需要在
// 在AppDelegate定義屬性
@property(nonatomic,unsafe_unretained)UIBackgroundTaskIdentifierbackgroundTaskIdentifier;
- (void)applicationWillResignActive:(UIApplication*)application;
里面加入以下的方法
- (void)applicationWillResignActive:(UIApplication*)application {
// 開啟后臺處理多媒體事件
[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession*session=[AVAudioSessionsharedInstance];
[session setActive:YESerror:nil];
// 后臺播放
[session setCategory:AVAudioSessionCategoryPlaybackerror:nil];
// 這樣做竿滨,可以在按home鍵進入后臺后 椅挣,播放一段時間,幾分鐘吧。但是不能持續(xù)播放網(wǎng)絡(luò)歌曲不傅,若需要持續(xù)播放網(wǎng)絡(luò)歌曲,還需要申請后臺任務(wù)id,具體做法是:
_backgroundTaskIdentifier=[AppDelegate backgroundPlayerID:_backgroundTaskIdentifier];
// 其中的_bgTaskId是后臺任務(wù)UIBackgroundTaskIdentifier _bgTaskId;
}
//實現(xiàn)一下backgroundPlayerID:這個方法:
+(UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId{
//設(shè)置并激活音頻會話類別
AVAudioSession*session=[AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryPlaybackerror:nil];
[session setActive:YESerror:nil];
//允許應(yīng)用程序接收遠程控制
[[UIApplicationsharedApplication] beginReceivingRemoteControlEvents];
//設(shè)置后臺任務(wù)ID
UIBackgroundTaskIdentifiernewTaskId=UIBackgroundTaskInvalid;
newTaskId=[[UIApplicationsharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid){
[[UIApplicationsharedApplication] endBackgroundTask:backTaskId];
}
returnnewTaskId;
}
到這里就可以進行后臺播報了,但是注意到這一步只有在程序沒有被殺死的情況下才可以播報, 殺死之后是不能播報的, 所有我們還要進行處理,這時需要使用 UNNotificationServiceExtension.
創(chuàng)建 UNNotificationServiceExtension
填寫文件名,邊創(chuàng)建好了
在NotificationService.m里面有一個方法
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void(^)(UNNotificationContent * _Nonnull))contentHandler;
此時把語音播報寫進去就可以了
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void(^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here...
self.bestAttemptContent.title = [NSStringstringWithFormat:@"%@ [modified]",self.bestAttemptContent.title];
NSString*content = request.content.userInfo[@"aps"][@"alert"][@"body"];
AVSpeechUtterance*utterance = [AVSpeechUtterancespeechUtteranceWithString:content];
AVSpeechSynthesizer*synth = [[AVSpeechSynthesizeralloc] init];
[synth speakUtterance:utterance];
self.contentHandler(self.bestAttemptContent);
}
認為到這里就完成了?,NO!在發(fā)送推送的時候還需要在極光推送服務(wù)里面配置一下
到這一步的時候,后臺播報就可以執(zhí)行了, 但是此播報服務(wù)只能在 iOS10 系統(tǒng)之后才可以進行, 如果想適配iOS9之前的只能做一個固定的音頻文件放到項目里面,比如支付寶的的到賬提醒, 然后在推送的時候
這時候就可以完美播放音頻文件了, 提醒:如果不需要動態(tài)的語音播放, 直接可以使用這個方法,不需要配置 UNNotificationServiceExtension 和后臺播放了,因為這個方法是系統(tǒng)認為推送的提示音