语音新闻播报在线收听

fangang 坐标: 87068 目录:婚姻家庭

- ( void ) didReceiveNotificationRequest: ( UNNotificationRequest * ) request withContentHandler: ( void ( ^ ) ( UNNotificationContent * _Nonnull ) ) contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [ request.content mutableCopy ] ; // iOS 10 在有语音播报的情况下 可屏蔽系统提示音,也可根据需求来 self.bestAttemptContent.sound = nil; // Modify the notification content here... // 获取共享域的偏好设置 NSUserDefaults *userDefault = [ [ NSUserDefaults alloc ] initWithSuiteName:@"group. xxx" ] ; // 解析推送自定义参数 userInfo NSDictionary *userInfo = [ self dictionaryWithUserInfo:self.bestAttemptContent.userInfo ] ; BOOL canSound = [ userDefault boolForKey:@"voice" ] ; NSString *voiceString = nil; if ( canSound ) { if ( [ refund condition ] ) { voiceString = [ NSString stringWithFormat:@" 退款 %@元!", userInfo [ @"money" ] ] ; } else { voiceString = [ NSString stringWithFormat:@" 收款 %@元!", userInfo [ @"money" ] ] ; } } // 语音合成 [ self syntheticVoice:voiceString ] ; self.contentHandler ( self.bestAttemptContent ) ;}

- ( void ) syntheticVoice: ( NSString * ) string { // 语音合成 self.synthesizer = [ [ AVSpeechSynthesizer alloc ] init ] ; AVSpeechUtterance *speechUtterance = [ AVSpeechUtterance speechUtteranceWithString:string ] ; // 设置语言类别(不能被识别,返回值为 nil) speechUtterance.voice = [ AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN" ] ; // 设置语速快慢 speechUtterance.rate = 0.55; // 语音合成器会生成音频 [ self.synthesizer speakUtterance:speechUtterance ] ;}

微信扫一扫

订阅每日移动开发及 APP 推广热点资讯

公众号:CocoaChina

专题栏目
最新