[英]Detect AVPlayerItem Status change
这是我的(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
所需的XamarinMediaManager
。
资料来源:
您正在尝试观察当前保存在CurrentItem
属性中的单个AVPlayerItem
实例,即使它当前为 null。
您可以在AVPlayer
实例上观察并使用您希望观察其变化的NSObject
的keyPath
:
obs = player.AddObserver("currentItem", NSKeyValueObservingOptions.New, (NSObservedChange obj) =>
{
Console.WriteLine($"{obj.NewValue}");
});
注意:持有参考。 到返回的IDisposable
否则你的观察者可能会超出范围并且|或者你会泄漏内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.