音频会话“躲避”在iOS 4中断了......? [英] Audio Session "Ducking" Broken in iOS 4...?

查看:124
本文介绍了音频会话“躲避”在iOS 4中断了......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MPAudioPlayerController访问iPod音乐库的应用程序,以及一个AVAudioPlayer,用于在音乐之上叠加音频。我用此文档作为指南。具体来说:

I've an app which uses the MPAudioPlayerController to access the iPod music library, and an AVAudioPlayer to overlay audio on top of the music. I've used this documentation as a guide. Specifically:


最后,您可以增强类别,以便在播放音频时自动降低其他音频的音量。例如,这可以用在锻炼应用中。当您的应用程序想要覆盖语音消息时,假设用户正在练习iPod - 例如,您已经划了10分钟。为了确保应用程序中的消息可以理解,请将kAudioSessionProperty_OtherMixableAudioShouldDuck属性应用于音频会话。当发生躲避时,设备上的所有其他音频 - 除了电话音频 - 音量降低。

Finally, you can enhance a category to automatically lower the volume of other audio when your audio is playing. This could be used, for example, in an exercise application. Say the user is exercising along to their iPod when your application wants to overlay a verbal message—for instance, "You’ve been rowing for 10 minutes." To ensure that the message from your application is intelligible, apply the kAudioSessionProperty_OtherMixableAudioShouldDuck property to the audio session. When ducking takes place, all other audio on the device—apart from phone audio—lowers in volume.

但我没有看到这种行为。事实上,我所看到的(或听到的)是,如果我将kAudioSessionProperty_OtherMixableAudioShouldDuck设置为true来设置AudioSession,则MPAudioPlayerController初始卷会减少,如果我然后在MPAudioPlayerController上调用pause(然后再次播放)音量级别增加到正常水平。播放AVAudioPlayer对音频级别没有任何影响...

But I'm not seeing this behavior. In fact, what I see (or hear, rather) is that if I setup the AudioSession with kAudioSessionProperty_OtherMixableAudioShouldDuck set to true, the MPAudioPlayerController initial volume gets reduced, and if I then call pause (and then play again) on the MPAudioPlayerController the volume level gets increased to "normal" levels. Playing the AVAudioPlayer does not have any affect on the audio level...

所以我设置了一个简单的测试用例来重现它。

So I've set up a simple test case to reproduce this.

在ViewController标题中:

In a ViewController header:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface MusicPlayerVolumeTestViewController : UIViewController <AVAudioPlayerDelegate>
{
    UIButton *musicButton;
    UIButton *soundButton;
    AVAudioPlayer *audioPlayer;
    MPMusicPlayerController *musicPlayerController;
}
@property (nonatomic, retain) IBOutlet UIButton *musicButton;
@property (nonatomic, retain) IBOutlet UIButton *soundButton;
@property (nonatomic, retain) MPMusicPlayerController *musicPlayerController;

- (IBAction)musicAction;
- (IBAction)soundAction;

@end

并且在实施中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Setup our Audio Session
    OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL);    
    //We want our audio to play if the screen is locked or the mute switch is on
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
    //We want our audio to mix with other app's audio
    UInt32 shouldMix = true;
    status = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (shouldMix), &shouldMix);
    //Enable "ducking" of the iPod volume level while our sounds are playing
    UInt32 shouldDuck = true;
    AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(shouldDuck), &shouldDuck);
    //Activate our audio session
    AudioSessionSetActive(YES);

    //Setup the Music Player to access the iPod music library
    self.musicPlayerController = [MPMusicPlayerController applicationMusicPlayer];
    [self.musicPlayerController setShuffleMode: MPMusicShuffleModeSongs];
    [self.musicPlayerController setRepeatMode: MPMusicRepeatModeNone];
    [self.musicPlayerController setQueueWithQuery:[MPMediaQuery songsQuery]];

    //Setup a AVAudioPlayer sound to overlay against the Music Player audio
    NSURL *soundURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"overlay" ofType:@"mp3"]];
    NSError *error = nil;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error: &error];
    if (!audioPlayer)
    {
        NSLog(@"Could not create audio effect player: %@", [error localizedDescription]);
    }
    [audioPlayer prepareToPlay];
}

- (IBAction)musicAction
{
    if (self.musicPlayerController.playbackState == MPMusicPlaybackStatePlaying)
    {
        [self.musicPlayerController pause];
    }
    else if (self.musicPlayerController.playbackState == MPMusicPlaybackStateStopped
          || self.musicPlayerController.playbackState == MPMusicPlaybackStatePaused)
    {
        [self.musicPlayerController play];
    }
}

- (IBAction)soundAction
{
    if (audioPlayer.playing)
    {
        [audioPlayer pause];
    }
    else
    {
        [audioPlayer play];
    }
}

我已经连接了几个UIButton。一个用于musicAction(用于播放/暂停MPMusicPlayerController)和一个用于soundAction(用于播放/暂停AVAudioPlayer)。

I've wired up a couple UIButtons. One for the musicAction (used for playing/pausing the MPMusicPlayerController) and one for the soundAction (used for playing/pausing the AVAudioPlayer).

如上所述,如果我点击musicAction按钮,音乐播放,但音量降低,如果我点击soundAction按钮,叠加播放,但不影响MPMusicPlayerController的音量。而且,更像bug的是,当我暂停然后播放MPMusicPlayerController时,如果我没有设置AudioSession,音乐音量会增加到原来的水平。

As mentioned, If I tap the musicAction button, the music plays, but at a reduced volume level, and if I tap the soundAction button, the overlay plays, but has no affect on the volume of the MPMusicPlayerController. And, more bug-like, is that when I pause and then play the MPMusicPlayerController the volume of the music increases to the level it would have been if I did not setup the AudioSession.

我很想知道是否有其他人有过这种经历,如果有的话,如果你找到了解决办法(或者可以告诉我,我做错了什么)。否则,我想我要去Radar。

I'm interested to know if anyone else has had this experience, and if so if you've found a work around (or can tell me that I'm doing something wrong). Otherwise, I guess I'm off to Radar.

非常感谢,

Levi

推荐答案

当我遇到类似的问题并且难以让它始终如一地工作时,我使用了这篇文章。它会工作一段时间,然后只是卡住躲避。我花了很多时间研究和调试这个,最后只是打电话给Apple。他们告诉我要查看面包屑示例代码。我按照那个例子,一切正常。

I used this post when I was having a similar issue and had trouble getting it to work consistently. It would work for a while and then just get stuck "ducked". I spent a lot of time researching and debugging this and finally just called Apple. They told me to look at the breadcrumb sample code. I followed that example and everything worked fine.

这是Apple的示例代码:

Here is Apple's sample code:

http://developer.apple.com/library/ios/#samplecode/Breadcrumb /Introduction/Intro.html

Apple的支持/开发人员还说要观察他们设置会话属性的方式和时间顺序。这显然是诀窍。在这里和其他地方阅读了很多帖子后,我的设置相互冲突。从头开始,遵循面包屑的例子使其成功。我从那时起就没有问题。

The support/developer at Apple also said to watch the order of how and when they set the session properties. That apparently was the trick. After reading a lot of posts here and elsewhere, I had settings that conflicted with each other. Starting from scratch and following the bread crumb example made it work. I have not had an issue since.

我在这里发布了相同的答案:

I posted the same answer here:

如何撤消AVAudioSession

给我这个答案是临界商业秘密,因为它很难开始工作,市场上有工作应用程序。对我来说,看到一个工作的例子,这几乎就是声音躲闪和脱落,值得用金重量。

To me this answer was borderline trade secret since it was so difficult to get working and there were working apps in the market. For me, seeing a working example, which was pretty much just the sound ducking and unducking was worth its weight in gold.

另外,我向Apple提到这是一个已知问题,他们不同意。他们在这里没有发现任何问题。果然,如果你遵循面包屑的例子,它可以解决任何奇怪的会话停用和反应等问题。

Also, I mentioned to Apple that this was a known issue and they did not agree. They were not aware of any issue here. Sure enough if you follow the breadcrumb example it works with out any strange session deactivate and reactive, etc., etc.

这篇关于音频会话“躲避”在iOS 4中断了......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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