- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个处理 AVPlayer(和 AVPlayerItem)的类,它向委托(delegate)人报告状态、时间和 timedMetadata。
除了大约 70-80% 的时间,初始 timedMetadata 不是“观察到的关键值”外,效果很好。然而,在错过第一个 timedMetadata 实例之后,似乎可以毫无问题地观察到所有其他 timedMetadata。
作为临时修复,我已经开始在视频的开头嵌入虚拟的 timedMetadata 标签,可以说除了“踢轮胎”什么都不做,之后一切正常。然而,这似乎很笨拙。我怀疑我是否以次优方式设置 AVPlayerItem 和 KVO,或者这里只是一个错误。
非常感谢任何关于为什么会发生这种情况的想法!下面的代码....
// CL: Define constants for the key-value observation contexts.
static const NSString *ItemStatusContext;
static const NSString *ItemMetadataContext;
static const NSString *ItemPlaybackForcastContext;
- (id)initWithURL:(NSURL *)url
{
if (self = [super init]) {
__weak TFPAVController *_self = self;
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:
^{
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
[item addObserver:_self forKeyPath:@"status" options:0 context:&ItemStatusContext];
[item addObserver:_self forKeyPath:@"timedMetadata" options:0 context:&ItemMetadataContext];
[item addObserver:_self forKeyPath:@"playbackLikelyToKeepUp" options:0 context:&ItemPlaybackForcastContext];
[[NSNotificationCenter defaultCenter] addObserver:_self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
_self.totalRunTime = CMTimeGetSeconds(item.duration);
[_self.delegate avPlayerNeedsView:player];
_self.playerItem = item;
_self.player = player;
}
else {
NSLog(@"The asset's tracks were not loaded: %@ // [%@ %@]",
error.localizedDescription,
NSStringFromClass([self class]),
NSStringFromSelector(_cmd));
}
_self.playerObserver = [_self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, _FrameRate_)
queue:NULL
usingBlock: ^(CMTime time) {
_self.currentVideoTime = CMTimeGetSeconds([_self.playerItem currentTime]);
}];
});
}];
}
return self;
}
#pragma mark - KVO Response Methods
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
__weak TFPAVController *_self = self;
if (context == &ItemStatusContext) {
dispatch_async(dispatch_get_main_queue(),
^{
if (((AVPlayerItem *)object).status == AVPlayerItemStatusReadyToPlay) {
[_self.delegate videoIsLoadedInPlayer:_self];
}
});
return;
}
else if (context == &ItemMetadataContext) {
dispatch_async(dispatch_get_main_queue(),
^{
[_self checkMetaDataForPlayerItem: (AVPlayerItem *)object];
});
return;
}
else if (context == &ItemPlaybackForcastContext) {
dispatch_async(dispatch_get_main_queue(),
^{
AVPlayerItem *playerItem = object;
if (CMTimeGetSeconds([playerItem currentTime]) <= 0) return;
NSDictionary *notificationDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:playerItem.playbackLikelyToKeepUp]
forKey:kAVPlayerStateKey];
[[NSNotificationCenter defaultCenter] postNotificationName:kAVPlayerNotification
object:self
userInfo:notificationDictionary];
});
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (void)checkMetaDataForPlayerItem:(AVPlayerItem *)item
{
NSMutableDictionary *metaDict = [NSMutableDictionary dictionary];
// CL: make sure there's stuff there
if (item.timedMetadata != nil && [item.timedMetadata count] > 0) {
// CL: if there is, cycle through the items and create a Dictionary
for (AVMetadataItem *metadata in item.timedMetadata) {
[metaDict setObject:[metadata valueForKey:@"value"] forKey:[metadata valueForKey:@"key"]];
}
// CL: pass it to the delegate
[self.delegate parseNewMetaData:[NSDictionary dictionaryWithDictionary:metaDict]];
}
}
最佳答案
啊哈,KVO。可能是 Apple 有史以来最糟糕的设计决策之一。
我猜它不再相关,但我猜你遇到的问题是,有时当你开始将自己添加为观察者时,你试图观察的值已经分配给了键,所以您的观察者选择器未被调用。
为避免这种情况,您可以在调用 addObserver:forKeyPath:options:context:
时将 NSKeyValueObservingOptionInitial
添加到 options
,您的观察者方法将使用当前值立即调用。
关于ios - AVPlayerItem 初始 timedMetadata 未被观察(KVO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065811/
当我的应用程序关闭时,主 Controller 类将其自身作为观察者从模型中删除,然后释放模型。像这样: - (void)dealloc { [theModel removeObserver:
我正在努力加深对这些机制的理解。我有一个启用触摸的 UIView,可以更新它自己的 center 属性。 我的理解是frame属性是center和bounds的合成函数。假设这是真的,我将一个观察者放
我正在尝试使用 KVO 来观察在我的页面 View Controller 的子内容 View Controller 的 ScrollView 中使用拖动时的更新变化,但是当应用程序启动时,它崩溃了说:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我尝试使用 KVO 观察 UIView 子类上的属性,以便通过调用 drawRect: 触发绘制。在我的 initWithFrame: 中,我有这个: ... self.observedKeysTha
我以前使用过 NSNotifications,但这是我第一次尝试在 Cocoa Touch 中使用 KVO。 我的 UITableView Controller 在各种数据源之间切换,因此我将它们封装
我遇到了 KVO 问题,但我不知道它是什么。 我有一个可用于编辑或创建记录的屏幕。该记录由一个模型对象表示,该对象包含许多 NSString 类型的变量。 该字符串的 Controller 有一个实例
这个问题已经有答案了: In Cocoa KVO, why doesn't a change on a NSMutableArray proxy notify observers? (1 个回答) 已
当我想观察保留属性的变化时,我经常遇到这样的情况: @interface AnObserver {…} @property(retain) Foo *foo; 现在,当我想要设置观察时,我需要编写自己
我定义了两个具有相应 ivars 的属性并合成了它们: @property (nonatomic,copy) NSString* title; @property (nonatomic,copy) N
我试图掌握绑定(bind),并在 xcode 中设置了一个带有 CPTextView 的 CPWindow,并将窗口和 textview 绑定(bind)到它们各自的导出。 我有点卡住了,但是试图让
一定有一些我对 KVO 没有了解的地方,我试图通过滚动 UIScrollView 来滚动 UITableView,偏移量转换是正确的,但是 UITableView 滚动发生在 UIScrollView
我正在学习 MVVM,所以我在属性上添加了观察者 init(model: RegisterUserModel = RegisterUserModel()) { self.registerMo
我的 iPhone 应用程序核心数据属性的自定义访问器方法有问题。我想做的不仅仅是取出原始类型并返回它。我有一个可能的 activityTypes 的 NSSet,如果对象没有 primitiveTy
事情是这样的:我有一个单例监视设备的事件存储的变化。我有一个名为 events 的属性,我已将其包装在 eventsSignal RACSignal 中。 _eventsSignal = [RACAb
我得到的 iOS 应用 实体很少,它们由关系链接。举一个简单的例子,假设我们有这样的关系(一对多): company > person 我正在使用 xcode 4.6 和用于模型生成的核心数据建模工具
我在 iOS 应用程序中遇到 KVO 问题。我似乎无法正确处理。我正在尝试做什么: 我有一个 MenuViewController(可在左侧显示,如 facebook 应用程序)观察(并显示)全局值。
我试图通过以下代码从其他对象观察 UIViewController 的 KVO 的界面方向: [((UIViewController *)self.delegate) addObserver:self
我正在尝试使用 KVO 在 UILabel 和对象数据之间创建一个小型绑定(bind)系统。如果我的 UI 发生变化,我的数据也必须发生变化,如果我的数据发生变化,我的 UI 应该刷新以显示新值。 我
如果我在我的 iOS 应用程序上设置了本地通知,我将更新一个按钮。我想用 KVO 做到这一点,因为可以通过多种方式设置通知,所以我将无法轻松跟踪本地通知何时发生更改,否则。 我是这样设置的:
我是一名优秀的程序员,十分优秀!