同时打2个AVPlayer音频文件时,音频干扰(Audio glitch when playing

2019-09-18 11:06发布

我有一个iOS应用程序播放一个AVPlayer背景配乐和“顶”起其他的声音片段与第二AVPlayer。 (声音片段都是从网上流,因此需要对AVPlayer)。问题是,第二AVPlayer开始播放时,它会导致后台AVPlayer停一秒钟,类似于在此评论中描述的一小部分:

播放多种音频文件与AVPlayer


我准备用这种方法的音频片段:

- (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的每个实例,使声音不融合的故障?

Answer 1:

这可能是因为软件编码器踢的结果。你会看到一个漂亮的大穗在内存中的软件编码器尝试播放第二个声音片段。 看看苹果的文档节“的iOS硬件和软件音频编解码器” http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html

“当使用硬件辅助解码,该装置可以在一个时间仅播放的支持的格式之一的单个实例。”

还......“为了与最佳性能播放多种声音,或者有效地播放声音而iPod在后台播放时,使用线性PCM(未压缩)或IMA4(压缩)的音频。”

如果你有在媒体编解码器,你可以不提供选择,看看在AudioUnit框架来获得更加无缝的混合。 特别是“音频混合器(MixerHost)”在http://developer.apple.com/library/ios/#samplecode/MixerHost/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010210



Answer 2:

我终于想通了什么问题。 我正在写在那里我调整使用AVMutableAudioMixInputParameters类的体积的应用,并且我试图通过向上或向下移动该卷上的每个样本使用此类标准化的音频。

而当只有少数产量的提高,这种方法的工作原理,它出现了斜坡的应用恰巧缓冲音频在同一线程上,所以当我用太多(>〜1000)他们,这最终吃了CPU应该是繁忙的缓冲声音,然后你在音频GGG毛刺。

我的解决办法是修改我的音量正常化算法使用较少的产量的提高,实现了同样的事情。 有一次,我能得到各组产量的提高,/曲下降到大约500左右,我不再有问题,和音频毛刺走了。



文章来源: Audio glitch when playing two AVPlayer audio files simultaneously