真機調(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];