ios 16 AVSpeechSynthesizer 沒聲音

真機調(diào)試用ios15測試文字轉(zhuǎn)語音是有聲音的,ios16下沒聲音

//需要轉(zhuǎn)換的文字
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"播放文字"];
utterance.rate = 0.5;// 語速,范圍0.0~1.0
utterance.pitchMultiplier = 1.5; // 音高,范圍0.5~2.0
utterance.volume = 1; // 音量,范圍0~1
 //設(shè)置發(fā)音,這是中文普通話
 AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
utterance.voice = voice;
//開始播放
AVSpeechSynthesizer *avSpeech = [[AVSpeechSynthesizer alloc] init];
avSpeech.delegate = self;    
 [avSpeech speakUtterance:utterance];

最后發(fā)現(xiàn),是IOS6設(shè)置了靜音,關(guān)閉靜音模式就可以播放了
需要在靜音模式下播放,只需在進入頁面時設(shè)置AVAudioSession即可

 //后臺播放 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
//靜音狀態(tài)下播放 
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//設(shè)置代理 可以處理電話打進時中斷音樂播放 
  [[AVAudioSession sharedInstance] setDelegate:self]; 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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