简体   繁体   English

AVPlayerViewController无法播放视频并显示Quicktime徽标

[英]AVPlayerViewController not playing video and showing Quicktime logo

I am trying to play video with new AVPlayerViewController introduced in XCode 6. 我正在尝试使用XCode 6中引入的新AVPlayerViewController播放视频。

To play video i have done this setup. 要播放视频,我已经完成了此设置。

  1. Using local mp4 file to play video 使用本地mp4文件播放视频

  2. Extended AVPlayerViewController 扩展的AVPlayerViewController

Player setup code : 播放器设置代码:

-(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 handling : 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");
        }
    }
}

Issue : 问题 :

KVO log is printing AVPlayerStatusReadyToPlay and file path seems ok. KVO日志正在打印AVPlayerStatusReadyToPlay,文件路径似乎正常。 Previously view was at least showing player with all default controls, but now without any changes it started to show Quick time logo without any control. 以前的视图至少显示了具有所有默认控件的播放器,但是现在没有任何更改,它开始显示没有任何控件的Quick time logo。 What is meaning of showing this logo ? 显示此徽标是什么意思? what i am doing wrong here ? 我在这里做错了什么?

Screenshot : 屏幕截图:

在此处输入图片说明

Here is exactly like what you wanted to do. 这与您想做的完全一样。 The problem with your code is that you are not using file path, use this to load the file path NSURL *url = [NSURL fileURLWithPath:urlString] 代码的问题是您没有使用文件路径,请使用它来加载文件路径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

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

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