iOS12.1推送語音播報沒有聲音問題及初步的解決方案

蘋果升級到iOS12.1的時候, 對于收款帶語音播報功能的App來說, 就是個災難. 據(jù)用戶反饋iOS12.1的手機收款沒有播報的聲音, 真操蛋, 剛上的新版本, 就發(fā)現(xiàn)了iOS12.1系統(tǒng)的手機不能播報語音了(其實是在拓展里的語音不能播報). 還以為就自己代碼的問題(怕被老板叼). 網(wǎng)上搜一波, 社區(qū)、博客里發(fā)現(xiàn)了大家都出現(xiàn)了這個問題, 現(xiàn)在可以確定是蘋果系統(tǒng)的問題了, 頓時心里輕松了一點, 不是自己的問題就好. 接下確定了問題了后, 就去尋找解決方案吧.

據(jù)收集到的情況, 拓展里原生語音合成、百度、訊飛這些語音都不可以播報, 不過有個大兄弟說有個方案可以iOS12.1語音播報問題, 后來和這位兄弟聊過后, 這個方案通過Background Audio, 蘋果審核不讓過的, 他們也舍棄了這個方案, 然后聊到播放固定音頻的方案, 其實也是自定了推送的鈴聲. 這個方案我也正在做的, 就我而言現(xiàn)在能達到的效果是, 手機在前臺和后臺都可以實現(xiàn)播報, 播報的是一個固定的音頻, 不能播報多少錢, 鎖屏后, 也可以推送和播報. 經(jīng)過深度測試(使用場景做參考), 當幾條推送信息同時到達(幾個用戶就是那么巧同時支付), 手機在鎖屏的情況下播報就會聲音重疊, 聽不出有幾條推送信息.

下面是代碼:

- (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;
}

上面就是我實現(xiàn)的代碼, 不知代碼是不是還不嚴謹才導致鎖屏用聲音重疊, 各位老兄有好的方案可以QQ我:1243600547.

更正上面貼出的代碼錯誤

這里對于iOS12.1及以上系統(tǒng)播報的效果主要是自定義鈴聲, 代碼已在上面更正了, 還有就是自定義鈴聲的音頻文件格式為: aiff,wav,caf, 其他的各位老鐵也可以試試.

********最新戰(zhàn)況*******

有大佬總結了得出三個方案:iOS12.1語音不播報問題

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

推薦閱讀更多精彩內(nèi)容