iOS12.1语音问题

iOS12.1推送语音播报没有声音问题及初步的解决方案

2018-11-16  本文已影响0人  耀敬业

苹果升级到iOS12.1的时候, 对于收款带语音播报功能的App来说, 就是个灾难. 据用户反馈iOS12.1的手机收款没有播报的声音, 真操蛋, 刚上的新版本, 就发现了iOS12.1系统的手机不能播报语音了(其实是在拓展里的语音不能播报). 还以为就自己代码的问题(怕被老板叼). 网上搜一波, 社区、博客里发现了大家都出现了这个问题, 现在可以确定是苹果系统的问题了, 顿时心里轻松了一点, 不是自己的问题就好. 接下确定了问题了后, 就去寻找解决方案吧.

据收集到的情况, 拓展里原生语音合成、百度、讯飞这些语音都不可以播报, 不过有个大兄弟说有个方案可以iOS12.1语音播报问题, 后来和这位兄弟聊过后, 这个方案通过Background Audio, 苹果审核不让过的, 他们也舍弃了这个方案, 然后聊到播放固定音频的方案, 其实也是自定了推送的铃声. 这个方案我也正在做的, 就我而言现在能达到的效果是, 手机在前台和后台都可以实现播报, 播报的是一个固定的音频, 不能播报多少钱, 锁屏后, 也可以推送和播报. 经过深度测试(使用场景做参考), 当几条推送信息同时到达(几个用户就是那么巧同时支付), 手机在锁屏的情况下播报就会声音重叠, 听不出有几条推送信息.

下面是代码:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    self.bestAttemptContent.title = @"到款通知";
    NSString *content = request.content.userInfo[@"aps"][@"alert"];
    
    if (@available(iOS 12.1, *)) {
        //自定义铃声
        self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"sound.wav"];
        self.contentHandler(self.bestAttemptContent);
        
        //各位老铁, 这里代码是问题的, 在拓展iOS12.1及以上版本里是不允许播放声音的, 下面的代码是没有任何作用的, 播报的效果主要是上面那里设置铃声的代码
//        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
//        NSURL *soundURL = [NSURL fileURLWithPath:soundFilePath];
//        SystemSoundID soundID;
//        AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &soundID);
//        AudioServicesPlaySystemSound(soundID);
//        AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
//            self.contentHandler(self.bestAttemptContent);
//        });
    }else{
        //播放合成音效
        self.bestAttemptContent.sound = nil;
        [[AVAudioSession sharedInstance] setActive:YES error:NULL];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [self playVoiceWithContent:content];
    }
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
    self.contentHandler(self.bestAttemptContent);
}

-(void)playVoiceWithContent:(NSString *)content{
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
    utterance.rate = 0.5;
    utterance.voice = self.synthesisVoice;
    [self.speechSynthesizer speakUtterance:utterance];
}

-(AVSpeechSynthesizer *)speechSynthesizer{
    if (_speechSynthesizer == nil) {
        _speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
        _speechSynthesizer.delegate = self;
    }
    return _speechSynthesizer;
}

-(AVSpeechSynthesisVoice *)synthesisVoice{
    if (_synthesisVoice == nil) {
        _synthesisVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
        
    }
    return _synthesisVoice;
}

上面就是我实现的代码, 不知代码是不是还不严谨才导致锁屏用声音重叠, 各位老兄有好的方案可以QQ我:1243600547.

更正上面贴出的代码错误

这里对于iOS12.1及以上系统播报的效果主要是自定义铃声, 代码已在上面更正了, 还有就是自定义铃声的音频文件格式为: aiff,wav,caf, 其他的各位老铁也可以试试.

********最新战况*******

有大佬总结了得出三个方案:iOS12.1语音不播报问题

上一篇下一篇

猜你喜欢

热点阅读