gpt4 book ai didi

ios - 设置 iOS MPMusicPlayerController 相对于 AVAudioPlayer 的音量

转载 作者:行者123 更新时间:2023-11-29 13:11:57 25 4
gpt4 key购买 nike

我有一个应用程序使用 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];

最佳答案

您需要学习如何“闪避”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);
}

关于ios - 设置 iOS MPMusicPlayerController 相对于 AVAudioPlayer 的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com