Flashes in AVPlayer when updating AVFragmentedAsset

I am using AVFoundation to capture video from the built-in camera to an AVFragmentedAsset on disk.

On a separate app I have an AVFragmentedAssetMinder that watches for changes in the asset duration and updates the AVPlayerItem of the AVPlayer.

The code doing the update is:
Code Block
CMTime oldTime = self.player.currentItem.currentTime;
float oldRate = self.player.rate;
self.player.rate = 0.0;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset: fragAsset];
[self.player replaceCurrentItemWithPlayerItem: playerItem];
[self.player seekToTime: oldTime toleranceBefore: kCMTimeZero toleranceAfter: kCMTimeZero];
self.player.rate = oldRate;

The problem with that approach is that I get flashes when updating, which is very annoying.

Is there any other way to update the playing AVPlayerItem?

I am using the latest macOS and Xcode.


Accepted Reply

After communication from Apple about the reported issue, the issue is resolved.

The solution for anyone having the same problem:

You just need to install an AVFragmentMinder after the asset hasFragments and duration properties are loaded. After that, you can create an AVPlayerItem with the fragmented asset and it will respect the growing duration as times goes by. Our using the AVAssetDurationDidChangeNotification callback to update the playerItem was superfluous.

Replies

What happens if you don't update the AVPlayerItem, but just let it continue playing from the AVFragmentedAsset that is growing due to the continuing capture?
Should that work automagically?

In our app there are a lot of dependencies between the AVPlayer and our custom controller through a periodicTimeObserver callback block. I commented them out and tried your suggestion, but the playback stops at the initial asset duration.


I removed all interdependencies between the AVPlayerView and other parts of our app and I tried again to play the asset without updating the AVPlayerItem. I have tried opening the asset both as an AVURLAsset and an AVFragmentedAsset.

Same result, the asset plays up to the duration it knew when it was first loaded (when the call to loadValuesAsynchronouslyForKeys: completed).

Anyone has any other ideas?
It sounds like there isn't an obvious solution for playing a growing fragmented asset without glitches. Will you file a bug using the feedback assistant and describe your use case? That will give us a chance to dig deeper, make sure there isn't a way to do this we haven't already thought of, and if necessary build a new capability into the playback system.
I just did: FB8829174
After communication from Apple about the reported issue, the issue is resolved.

The solution for anyone having the same problem:

You just need to install an AVFragmentMinder after the asset hasFragments and duration properties are loaded. After that, you can create an AVPlayerItem with the fragmented asset and it will respect the growing duration as times goes by. Our using the AVAssetDurationDidChangeNotification callback to update the playerItem was superfluous.