I have an iOS app that plays a background soundtrack with one AVPlayer and plays other sound clips "on top" with a second 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:
Play multiple Audio Files with 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];
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?