AVPlayer没有显示任何内容

Dav*_*vis 7 youtube objective-c ios avplayer avplayerlayer

我尝试在youtube vimeo中嵌入不同的视频,dailymotion.

可悲的是,除了我的containerView的backgroundcolor之外,没有显示任何内容:

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];

//item.url is my url which i get fro my webserver, it looks like http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player

            AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:item.url]];

            AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];

            NSLog(@"%@",playerItem);

            AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

            avPlayerLayer.frame = self.frame;
            [containerView.layer addSublayer:avPlayerLayer];
            [self addSubview:containerView];
            [avPlayer play];

            if (avPlayer.status == AVPlayerStatusReadyToPlay) {
                //[playingLbl setText:@"Playing Audio"];
                NSLog(@"It works");

            } else if (avPlayer.status == AVPlayerStatusFailed) {
                // something went wrong. player.error should contain some information
                NSLog(@"Not works");
                NSLog(@"%@",avPlayer.error);             
            }
            else if (avPlayer.status == AVPlayerItemStatusUnknown) {
                NSLog(@"AVPlayer Unknown");
            }

            containerView.backgroundColor = [UIColor blueColor];
            NSLog(@"error: %@", avPlayer.error);
            NSLog(@"AVPlayer: %@", avPlayer);
Run Code Online (Sandbox Code Playgroud)

AVPlayer错误是空的,我唯一从状态获取的Log是:AVPlayerItemStatusUnknown.有任何想法吗?

编辑1:

Ich将我的代码更改为:

@implementation VideoView

BlockVideo *list;

- (id)initWithBlock:(GFBlock *)block {
    self = [super initWithBlock:block];

if (self) {

    if (block.values && block.values.count) {
        list = (GFBlockVideo *) [block.values objectAtIndex:0];

        for (int i=0; i<list.videos.count; ++i) {

            GFBlockVideoItem *item = list.videos[i];
            UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, 320.0f, 200.0f)];


//Like i said item.url = http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player
//@property (nonatomic, strong) NSString* url;

             AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:item.url]];
             AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
             AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
             AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

             avPlayerLayer.frame = containerView.frame;
            [containerView.layer addSublayer:avPlayerLayer];
            [self addSubview:containerView];
            [avPlayer play];

             containerView.backgroundColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)

可悲的是,我唯一能看到的是蓝色容器视图:/

在此输入图像描述

我认为问题不在于AVPlayer本身,但框架和层可能......

Unh*_*lig 9

您必须执行以下操作:

AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:item.url]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

avPlayerLayer.frame = self.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self addSubview:containerView];
[avPlayer play];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

附录:

它还取决于您的URL; 如果你说的话,你有一个这样的格式:

http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player

然后你应该使用它:

[NSURL urlWithString:item.url]; 
Run Code Online (Sandbox Code Playgroud)

鉴于这item是您的对象,并且url是对象类型的属性NSString.

  • 是的,似乎我必须使用UIWebView for youtube vimeo and Co.,谢谢你的帮助! (2认同)