真機(jī)調(diào)試用ios15測(cè)試文字轉(zhuǎn)語(yǔ)音是有聲音的冈在,ios16下沒(méi)聲音
//需要轉(zhuǎn)換的文字
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"播放文字"];
utterance.rate = 0.5;// 語(yǔ)速礼搁,范圍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;
//開(kāi)始播放
AVSpeechSynthesizer *avSpeech = [[AVSpeechSynthesizer alloc] init];
avSpeech.delegate = self;
[avSpeech speakUtterance:utterance];
最后發(fā)現(xiàn)表悬,是IOS6設(shè)置了靜音,關(guān)閉靜音模式就可以播放了
需要在靜音模式下播放炉旷,只需在進(jìn)入頁(yè)面時(shí)設(shè)置AVAudioSession即可
//后臺(tái)播放
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
//靜音狀態(tài)下播放
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//設(shè)置代理 可以處理電話打進(jìn)時(shí)中斷音樂(lè)播放
[[AVAudioSession sharedInstance] setDelegate:self];