相对于AVAudioPlayer设置iOS MPMusicPlayerController音量 [英] Setting iOS MPMusicPlayerController volume relative to AVAudioPlayer

查看:87
本文介绍了相对于AVAudioPlayer设置iOS MPMusicPlayerController音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用MPMusicPlayerController播放背景音乐,并使用AVAudioPlayer播放前景声音.我想控制相对音量,以使MPMusicPlayerController降低很多,但是设置音量会改变整个系统的音量,就好像使用手机侧面的按钮一样.有没有办法降低MPMusicPlayerController的体积而不降低系统体积?

I have an app that plays background music using MPMusicPlayerController and foreground sounds using AVAudioPlayer. I want to control the relative volume so that MPMusicPlayerController is much lower, but setting the volumne changes the overall system volume as if using the buttons on the side of the phone. Is there way to lower the volumne of MPMusicPlayerController without lowering the system volume?

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之前和之后,请尝试调用下面的方法.

You need to learn how to "duck" the MPMusicPLayer. Try calling the method below before and after you play the AVAudioPlayer.

基本上,[self setAudioSessionWithDucking:YES]在此之前,[self setAudioSessionWithDucking:NO]在此之后.

Basically, [self setAudioSessionWithDucking:YES] before and [self setAudioSessionWithDucking:NO] after.

- (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);
}

这篇关于相对于AVAudioPlayer设置iOS MPMusicPlayerController音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆