繁体   English   中英

检测 AVPlayerItem 状态变化

[英]Detect AVPlayerItem Status change

这是我的(Xamarin C#)代码:( playerAVPlayer 。)

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

资料来源:

  1. 使用 Xamarin 的 MediaManager 插件播放音频和视频
  2. Plugin.MediaManager NuGet
  3. GitHub

您正在尝试观察当前保存在CurrentItem属性中的单个AVPlayerItem实例,即使它当前为 null。

您可以在AVPlayer实例上观察并使用您希望观察其变化的NSObjectkeyPath

例子:

obs = player.AddObserver("currentItem", NSKeyValueObservingOptions.New, (NSObservedChange obj) =>
{
    Console.WriteLine($"{obj.NewValue}");
});

注意:持有参考。 到返回的IDisposable否则你的观察者可能会超出范围并且|或者你会泄漏内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM