1

我有一个使用 MPMusicPlayerController 播放背景音乐和使用 AVAudioPlayer 播放前景声音的应用程序。我想控制相对音量以使 MPMusicPlayerController 低得多,但设置音量会改变整个系统音量,就像使用手机侧面的按钮一样。有没有办法在不降低系统音量的情况下降低 MPMusicPlayerController 的音量?

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 0.1; // at this point the overall system volune has been set to 0.1
[musicPlayer setQueueWithItemCollection:collection];
[musicPlayer play];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
[player prepareToPlay];
[player setVolume: 1.0]; // I want this to be twice as loud as MPMusicPlayerController
[player play];
4

1 回答 1

1

您需要学习如何“躲避” MPMusicPLayer。在播放 AVAudioPlayer 之前和之后尝试调用以下方法。

基本上,[self setAudioSessionWithDucking:YES] 之前和 [self setAudioSessionWithDucking:NO] 之后。

- (void)setAudioSessionWithDucking:(BOOL)isDucking
{
     AudioSessionSetActive(NO);

    UInt32 overrideCategoryDefaultToSpeaker = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (overrideCategoryDefaultToSpeaker), &overrideCategoryDefaultToSpeaker);

    UInt32 overrideCategoryMixWithOthers = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (overrideCategoryMixWithOthers), &overrideCategoryMixWithOthers);

    UInt32 value = isDucking;
    AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(value), &value);

    AudioSessionSetActive(YES);
}
于 2013-10-07T07:08:13.313 回答