1

我的应用程序想要在可用时播放一些文本,如果在后台播放一些音乐,我想在我的应用程序播放文本时降低音乐的声音,我所做的是:

[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:AVAudioSessionCategoryOptionDuckOthers
                                      error:&err];
[[AVAudioSession sharedInstance] setActive:YES error:&err];

该选项AVAudioSessionCategoryOptionDuckOthers将在我的应用程序播放其文本时降低音乐音量。然后用 , 播放文本,然后speechSynthesizer在:

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)

我做的话语:

[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

所以音乐将恢复到原来的音量。

通过将会话活动设置为 NO 的问题,我失去了音量控制(iPhone 硬件音量控制在手机左侧)。即,我无法提高或降低我的应用程序的音量,除非在我更改音量时我的应用程序中正在播放文本。

4

2 回答 2

2

非常感谢,如果我在播放文本之前这样做,它会起作用:

[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                 withOptions:AVAudioSessionCategoryOptionDuckOthers
                                       error:nil];

当我第二次播放我的文字时,我不得不setActive:NO像没有它一样,音乐将暂停!!

然后在我播放我的文字之后,我这样做:

[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: 0 error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
于 2014-04-23T20:35:27.893 回答
0

setActive:设置为NO后立即重新设置YES!将类别设置为环境,以便背景声音可以继续播放。

所以,在你玩之前:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
                                 withOptions: AVAudioSessionCategoryOptionDuckOthers
                                       error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];

玩后:

[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
                                 withOptions: 0
                                       error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
于 2014-04-22T21:48:57.087 回答