简体   繁体   English

同时播放两个AVPlayer音频文件时出现音频故障

[英]Audio glitch when playing two AVPlayer audio files simultaneously

I have an iOS app that plays a background soundtrack with one AVPlayer and plays other sound clips "on top" with a second AVPlayer. 我有一个iOS应用程序,播放一个AVPlayer的背景音轨,并播放其他声音剪辑“在顶部”与第二个AVPlayer。 (The sound clips are streamed from the Internet, hence the requirement for AVPlayer.) The problem is that when the second AVPlayer starts playing, it causes the background AVPlayer to stop for a fraction of a second, similar to what's described in this comment: (声音片段是从互联网流式传输的,因此需要AVPlayer。)问题是,当第二个AVPlayer开始播放时,它会导致背景AVPlayer停止片刻,类似于此评论中描述的内容:

Play multiple Audio Files with AVPlayer 使用AVPlayer播放多个音频文件


I am preparing the audio clips with this method: 我正在使用这种方法准备音频剪辑:

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

...and then invoking [player play]; ...然后调用[player play]; when I want to hear each sound. 当我想听到每个声音。


Is there something I need to do when I set up the audio session or each instance of the AVPlayer, so that the sounds blend without the glitch? 当我设置音频会话或AVPlayer的每个实例时,我需要做些什么,以便声音在没有故障的情况下融合?

This is probably as a result of the software encoder kicking in. You'll see a pretty big spike in memory as the software encoder attempts to play the second sound clip. 这可能是因为软件编码器开始使用。当软件编码器尝试播放第二个声音片段时,你会看到内存中出现相当大的峰值。 Take a look at the apple docs section "iOS Hardware and Software Audio Codecs" http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html 看看苹果文档部分“iOS硬件和软件音频编解码器” http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

"When using hardware-assisted decoding, the device can play only a single instance of one of the supported formats at a time." “当使用硬件辅助解码时,设备一次只能播放一种支持格式的单个实例。”

Also... "To play multiple sounds with best performance, or to efficiently play sounds while the iPod is playing in the background, use linear PCM (uncompressed) or IMA4 (compressed) audio." 另外......“要播放具有最佳性能的多种声音,或者在iPod在后台播放时有效播放声音,请使用线性PCM(未压缩)或IMA4(压缩)音频。”

If you have no choice in the media codecs you can provide, take a look at the AudioUnit framework to get a more seamless mixing. 如果您无法选择可以提供的媒体编解码器,请查看AudioUnit框架以获得更加无缝的混音。 In particular the "Audio Mixer (MixerHost)" at http://developer.apple.com/library/ios/#samplecode/MixerHost/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010210 特别是http://developer.apple.com/library/ios/#samplecode/MixerHost/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010210上的“音频混音器(MixerHost)”

I finally figured out what the problem is. 我终于弄明白了问题所在。 I'm writing an application where I'm adjusting the volume using the AVMutableAudioMixInputParameters class, and I'm attempting to normalize audio using this class by moving the volume up or down on a per-sample basis. 我正在编写一个应用程序,我正在使用AVMutableAudioMixInputParameters类调整音量,我试图通过在每个样本的基础上向上或向下移动音量来使用此类来标准化音频。

While this approach works when there are only a few volume ramps, it appears that the application of the ramps happens on the same thread that buffers the audio, so when I use too many (>~1000) of them, this eventually eats up the CPU that should be busy buffering audio, and then you have a ggg-glitch in the audio. 虽然这种方法在只有少量音量斜坡时有效,但似乎斜坡的应用发生在缓冲音频的同一个线程上,所以当我使用太多(> ~1000)它们时,这最终会消耗掉应该忙于缓冲音频的CPU,然后你在音频中有一个ggg-glitch。

My solution was to refactor my volume normalization algorithm to use fewer volume ramps to achieve the same thing. 我的解决方案是重构我的音量标准化算法,以使用更少的音量斜率来实现同样的目的。 Once I was able to get each set of volume ramps/song down to approximately 500 or so, I no longer had the issue, and the audio glitch went away. 一旦我能够将每组音量斜坡/歌曲降低到大约500左右,我就不再有问题了,音频故障就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM