我有一个问题,我想让 iPod 在后台播放音乐,在我的应用程序中,我想AVSpeechUtterance
降低 iPod 的音量,播放一串文本,然后我想让 iPod 继续播放。我尝试了几件事,但从未有过 AVAudioSession 并且AVSpeechUtterance
可以很好地协同工作。我可以让 iPod 停止,暂停一会儿,然后重新开始播放。我可以看到我的字符串被传递给AVSpeechUtterance
,但它不会优先于扬声器播放。这是我的代码:
查看确实加载了:
- (void)viewDidLoad
{
//set the audio session for my app to play from the TTS library
NSError *error;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:1 error:&error];
[audioSession setActive:YES error:&error];
if(error){
NSLog(@"Audio Error %@", error);
}
syn = [[AVSpeechSynthesizer alloc] init];
syn.delegate = self;
iPodMusicPlayer = [[MPMusicPlayerController alloc] init];
}
这是我的语音功能:
-(void)speech:(NSString *)string
{
if(audioSession.isOtherAudioPlaying){
wasPlaying = YES;
NSLog(@"Other audio is playing");
[iPodMusicPlayer pause];
}
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:string];
utterance.rate = 0.3f;
utterance.volume = 1.0;
[syn speakUtterance:utterance];
}
AVSpeechSynthesizer
演讲结束后调用的委托方法
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
if(wasPlaying){
NSLog(@"Start Playing");
[iPodMusicPlayer play];
wasPlaying = NO;
}
}
AVAudioSession
我在这里查看了 Apple 文档,看起来我已经正确实施了所有内容。有谁知道我哪里出错了?