You can get the player's current item and add the observer to it.
The property:
private AVPlayerItem CurrentItem => Player.CurrentItem;
The observer:
CurrentItem.AddObserver(this, new NSString("status"),
NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial,
StatusObservationContext.Handle);
If you want, you can also attach loadedTimeRanges:
CurrentItem.AddObserver(this, new NSString("loadedTimeRanges"),
NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New,
LoadedTimeRangesObservationContext.Handle);
NB: Always be sure to detach the observer for the status before attaching:
CurrentItem.RemoveObserver(this, new NSString("status"));
Edit: StatusObservationContext
is simply a property, pointing to status
string.
public static readonly NSString StatusObservationContext = new NSString("Status");
Useful resources
You can find everything that you may need for your AVPlayer
implementation in Martinj's excellent package - XamarinMediaManager
.
Sources:
- Play Audio and Video with the MediaManager Plugin for Xamarin
- Plugin.MediaManager NuGet
- GitHub