iOS 在允許的時間條件下的后臺語音播報

? ? 由于公司的需求要求搞一個類似于支付寶那樣的后臺推送的語音播報,研究了下文字轉語音,以及在iOS允許的一定時間范圍內的后臺播報語音。

? ? 1.首先來講講iOS 10.0 的推送,這里就不用什么友盟,極光了。直接看文檔用原生的推送。



speech 是我自己簡單封裝的語音播報類,在git 上有demo

@interface AppDelegate ()@property (nonatomic, strong) TTSpeech *speech;

@property (assign, nonatomic)UIBackgroundTaskIdentifier backIden;//后臺運行的標識

@end

@implementation AppDelegate

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

UNAuthorizationOptions types10=UNAuthorizationOptionBadge|UNAuthorizationOptionSound;

//用戶權限申請

[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {

? ? if (granted) {

? ? ? ? //點擊允許

? ? } else {

? ? ? ? //點擊不允許

? ? }

}];

基本設置就這些,注冊完了.

//iOS 10 新增2個代理方法

//前臺推送過來的時候調用的方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {


}

//后臺推送的時候調用的方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler ?{


}

//靜默推送回調的方法,后臺推送 的字段"content-available":"1" 一定要加 ,否則在后臺推送信息代碼不會走!

{"aps":{"alert":"This is some fancy message.","badge":8,"sound": "defalut","content-available":"1"}}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

? ? completionHandler(UIBackgroundFetchResultNewData);

? ? [_speech speakWithText:userInfo[@"aps"][@"alert"]];?

? ? NSLog(@"userInfo--%@--", userInfo);

}

重點來了,我們需要申請iOS 后臺一段時間,(最起碼在iOS 后臺允許情況下)

- (void)applicationDidEnterBackground:(UIApplication *)application

{

? ? _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

? ? //后臺結束(被懸掛起)時候調用block ,此時也需要和iOS 系統說下,結束此次借用后臺時間(有借有還!)

? ? [[UIApplication sharedApplication] endBackgroundTask:_backIden];

}];

}?

自己實際測試,如果不借用后臺,那么即使后臺允許播報語音也會被系統的一些中斷通知中斷掉。

TTSpeech .h

#import@interface TTSpeech : NSObject

- (void)speakWithText:(NSString *)text;//語音播報

@end

TTSpeech.m

TTSpeech (){

? ? AVAudioSession *_session;

? ? AVSpeechSynthesizer *_synth;

}

//@property (nonatomic, strong) IFlySpeechSynthesizer *iFlySpeechSynthesizer;

@end

@implementation TTSpeech

- (instancetype)init {

? ? self = [super init];

? ? if (self) {

? ? [self config];

? ? [self openBackground:YES];

? ? }

? ? return self;

}

- (instancetype)initWithBackgroundModes:(BOOL)background {

? ? self = [super init];

? ? if (self) {

? ? [self config];

? ? [self openBackground:background];

? ? }

? ? return self;

}

- (void)config {

? ? _session = [AVAudioSession sharedInstance];

? ? _synth? = [[AVSpeechSynthesizer alloc] init];

? ? _synth.delegate = self;

? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlerleInterruption:) ? ? name:AVAudioSessionInterruptionNotification object:_session];

- (void)handlerleInterruption:(NSNotification *)note {

? ? NSLog(@"%d", [_synth continueSpeaking]);

}

#pragma mark - 是否申請后臺語音播報

- (void)openBackground:(BOOL)background {

? ? NSError *error = NULL;

? ? [_session setCategory:AVAudioSessionCategoryPlayback error:&error];

? ? if(error) {

? ? // Do some error handling

? ? NSLog(@"后臺播報錯誤");

? ? }

? ? [_session setActive:YES error:&error];

? ? if (error) {

? ? // Do some error handling

? ? NSLog(@"后臺播報錯誤");

? ? }

}

- (void)speakWithText:(NSString *)text {

? ? AVSpeechUtterance *utterance? = [[AVSpeechUtterance alloc] initWithString:text];

? ? utterance.rate = 0.5;

? ? utterance.volume = 0.6;

? ? AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice ? ? ? voiceWithIdentifier:@"com.apple.ttsbundle.siri_female_zh-CN_premium"];//優化音質

? ? utterance.voice = voice;

? ? [_synth speakUtterance:utterance];

}

#pragma mark - 語音播報代理

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {

? ? NSLog(@"%s",__func__);

}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {

? ? NSLog(@"%s",__func__);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容