AVPlayerViewController不播放视频并显示Quicktime徽标

时间:2014-12-07 15:59:55

标签: ios xcode6 avplayer

我正在尝试使用XCode 6中引入的新AVPlayerViewController播放视频。

要播放视频我已完成此设置。

  1. 使用本地mp4文件播放视频

  2. 扩展AVPlayerViewController

  3. 播放器设置代码:

    -(void)setupPlayer
    {
        NSString* filePath = [[NSBundle mainBundle] pathForResource:@"exodus_trailer" ofType:@"mp4"];
        NSLog(@"File Path : %@",filePath);
        AVAsset *avAsset = [AVAsset assetWithURL:[NSURL URLWithString:filePath]];
        AVPlayerItem *avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
        self.player = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
        [self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
        [self.player play];
    }
    

    KVO处理:

    - (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 Failed");
            }
            else if (self.player.status == AVPlayerStatusReadyToPlay)
            {
                NSLog(@"AVPlayerStatusReadyToPlay");
               [self.player play];
            }
            else if (self.player.status == AVPlayerItemStatusUnknown)
            {
                NSLog(@"AVPlayer Unknown");
            }
        }
    }
    

    问题:

    KVO日志正在打印AVPlayerStatusReadyToPlay,文件路径似乎正常。之前的视图至少显示了所有默认控件的播放器,但现在没有任何更改,它开始显示没有任何控制的Quick time徽标。显示此徽标的含义是什么?我在这做错了什么?

    屏幕截图:

    enter image description here

1 个答案:

答案 0 :(得分:1)

这与您想要做的完全一样。您的代码的问题是您没有使用文件路径,使用它来加载文件路径 NSURL * url = [NSURL fileURLWithPath:urlString]

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"testfile" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:urlString];

    self.player = [[AVPlayer alloc] initWithURL:url];
    [self.player addObserver:self forKeyPath:@"status"
                                      options:NSKeyValueObservingOptionNew
                                      context:NULL];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.player) {
        AVPlayerStatus status = [change[NSKeyValueChangeNewKey] integerValue];
        if (status == AVPlayerStatusReadyToPlay) {
            [self.player play];
            [self.player removeObserver:self forKeyPath:@"status"];
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

@end