同时播放两个AVPlayeraudio文件时出现audio故障

我有一个iOS应用程序,用一个AVPlayer播放背景音轨,并用另一个AVPlayer播放其他声音片段。 (声音剪辑是从互联网stream传输的,因此需要AVPlayer。)问题是,当第二个AVPlayer开始播放时,它会导致后台AVPlayer停止几分之一秒,类似于此评论中所描述的内容:

用AVPlayer播放多个audio文件

我正在准备这个方法的audio剪辑:

- (void)prepareAudio:(NSURL *)assetURL { asset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; playerItem = [AVPlayerItem playerItemWithAsset:asset]; [player replaceCurrentItemWithPlayerItem:playerItem]; [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil]; [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFailedToReachEnd:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:[player currentItem]]; } 

…然后调用[player play]; 当我想听到每个声音。

当我设置AVPlayer的audio会话或每个实例时,是否有需要做的事情,以便声音混合没有毛刺?

这可能是由于软件编码器踢入的结果。当软件编码器尝试播放第二个声音片段时,会看到内存相当大的峰值。 看看苹果文档部分“iOS硬件和软件audio编解码器” http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

“使用硬件辅助解码时,设备一次只能播放一种支持格式的单一实例。”

另外…“要以最佳性能播放多个声音,或者在iPod在后台播放时高效播放声音,请使用线性PCM(未压缩)或IMA4(已压缩)audio。

如果您没有select可以提供的媒体编解码器,请查看AudioUnit框架以获得更加无缝的混音。 特别是http://developer.apple.com/library/ios/#samplecode/MixerHost/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010210中的“audio混音器(MixerHost)”

我终于明白了问题所在。 我正在编写一个应用程序,使用AVMutableAudioMixInputParameters类调整音量,我试图通过每个采样基础上移或下移音量来使用此类对audio进行标准化。

虽然这种方法在音量斜坡只有less量的情况下工作,但似乎斜坡的应用发生在缓冲audio的同一线程上,所以当我使用太多(>〜1000)时,这最终会消耗掉CPU应该忙于缓冲audio,然后在audio中出现ggg-glitch。

我的解决scheme是重构我的音量标准化algorithm,使用更less的音量斜坡来实现同样的事情。 一旦我能够将每个音量斜坡/歌曲降到大约500左右,我就不再有这个问题了,audio故障就消失了。