我有一个使用AVAudioPlayer使用MPMusicPlayerController和前景声音播放背景音乐的应用程序。我想控制相对音量,以便MPMusicPlayerController低得多,但设置volumne会改变整个系统音量,就像使用手机侧面的按钮一样。有没有办法降低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];
答案 0 :(得分: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);
}