R B*_*nan 5 objective-c ios ios8.1
知道为什么我可以让我的AVPlayerViewController类播放内容,但无法让它显示播放控件吗?播放器按预期加载内容,但就是这样.
在我的MediaPlayerViewController.m类中,我有:
#pragma mark - Player
- (void)setupPlayer{
// Sample URLs to use either a local file or streaming URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Besan" withExtension:@"mp4"];
//url = [NSURL URLWithString:@"http://www.youtube.com/watch?v=qcI2_v7Vj2U"];
// Create an instance of the AVPlayer object
self.player = [AVPlayer playerWithURL:url];
// Keep an eye on the status of our player
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if (object == self.player && [keyPath isEqualToString:@"status"]) {
if (self.player.status == AVPlayerStatusFailed){
NSLog(@"AVPlayer setup failed");
} else if (self.player.status == AVPlayerStatusReadyToPlay) {
NSLog(@"AVPlayer is ready to play");
[self.player play];
}
}
}
Run Code Online (Sandbox Code Playgroud)
hol*_*ann 13
它不是框架错误,而是与键值观察的工作方式有关.通过实施
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
Run Code Online (Sandbox Code Playgroud)
在您的ViewController中,您将覆盖AVPlayerViewController的实现.解决方案是在注册观察者时添加上下文:
static NSString* const AVPlayerStatusObservationContext = @"AVPlayerStatusObservationContext";
Run Code Online (Sandbox Code Playgroud)
然后像这样注册你的观察者
[self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionPrior context:(__bridge void *)(AVPlayerStatusObservationContext)];
Run Code Online (Sandbox Code Playgroud)
并在observeValueForKeyPath中执行以下操作
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == (__bridge void *)(AVPlayerStatusObservationContext))
{
NSLog(@"Change Object %@, Value %@",object,change);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Run Code Online (Sandbox Code Playgroud)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 正如我在对问题的评论中指出的那样,当您在 AVPlayerViewController 中实现时,控件会消失。它可以是一个空的实现;播放器控件不会出现。这一定是一个框架错误,我很震惊(震惊!)苹果没有发现这一点。
我的解决方案是在不同的类中实现观察者。我创建了一个继承自 NSObject 的 VideoObserver 类,并将其设置如下:
@implementation VideoObserver
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"status"]) {
NSLog(@"Change: %@", change);
}
}
@end
Run Code Online (Sandbox Code Playgroud)
在 AVPlayerViewController 中,我将观察者设置为属性,然后使用它开始观察:
self.observer = [VideoObserver new];
NSURL * videoURL = [NSURL URLWithString:@"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"];
self.videoPlayer = [AVPlayer playerWithURL:videoURL];
[self.videoPlayer addObserver:self.observer forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil];
Run Code Online (Sandbox Code Playgroud)
我已经在 Github 上发布了我的演示项目。
| 归档时间: |
|
| 查看次数: |
3811 次 |
| 最近记录: |