这是我的 (Xamarin C#) 代码:(player
是一个 AVPlayer
。)
var v = NSNotificationCenter.DefaultCenter.AddObserver(new NSString("Status"), ReadyNow, player.CurrentItem);
它从不调用 ReadyNow(NSNotification obj)
,即使播放器播放正常也是如此。
如何修复它以调用该方法? (我不知道错误是在 Xamarin/C# 部分还是在我正在使用的对象等中。如果用 Swift 编写,这将是相同的错误。)
最佳答案
您可以获取玩家的当前元素并将观察者添加到其中。
属性:
private AVPlayerItem CurrentItem => Player.CurrentItem;
观察者:
CurrentItem.AddObserver(this, new NSString("status"),
NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial,
StatusObservationContext.Handle);
如果需要,也可以附上loadedTimeRanges :
CurrentItem.AddObserver(this, new NSString("loadedTimeRanges"),
NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New,
LoadedTimeRangesObservationContext.Handle);
注意:始终确保在附加之前分离观察者的状态:
CurrentItem.RemoveObserver(this, new NSString("status"));
编辑:StatusObservationContext
只是一个属性,指向 status
字符串。
public static readonly NSString StatusObservationContext = new NSString("Status");
有用的资源
您可以在 Martinj 的优秀包 - XamarinMediaManager
中找到您的 AVPlayer
实现可能需要的一切。
来源:
关于c# - 检测 AVPlayerItem 状态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57810910/