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

bru*_*337 10 audio ios avplayer

我有一个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]];
 }
Run Code Online (Sandbox Code Playgroud)

......然后[player play];在我想听到每个声音的时候调用.


当我设置音频会话或AVPlayer的每个实例时,我需要做些什么,以便声音在没有故障的情况下融合?

Eoi*_*oin 2

这可能是由于软件编码器启动的结果。当软件编码器尝试播放第二个声音剪辑时,您会看到内存出现相当大的峰值。看看苹果文档部分“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