Chr*_*s C 5 key-value-observing avfoundation ios avplayer
苹果文档提到了它,但是如何为AVPlayerItem的loadedTimeRanges属性设置键值观察?该属性是一个不会改变的NSArray,所以你不能只使用它playerItem addObserver:self forKeyPath:@"loadedTimeRanges ...
或者是否有其他方法可以在此更改时收到通知或更新?
实际上,我正在使用KVO for loadedTimeRanges而没有任何麻烦.也许你只是没有设置正确的选项?以下是对Apple AVPlayerDemo中某些代码的一个非常小的修改,它对我来说非常好用.
//somewhere near the top of the file
NSString * const kLoadedTimeRangesKey = @"loadedTimeRanges";
static void *AudioControllerBufferingObservationContext = &AudioControllerBufferingObservationContext;
- (void)someFunction
{
// ...
//somewhere after somePlayerItem has been initialized
[somePlayerItem addObserver:self
forKeyPath:kLoadedTimeRangesKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:AudioControllerBufferingObservationContext];
// ...
}
- (void)observeValueForKeyPath:(NSString*) path
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if (context == AudioControllerBufferingObservationContext)
{
NSLog(@"Buffering status: %@", [object loadedTimeRanges]);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6284 次 |
最近记录: |