AVPlayer没有显示任何内容。

8

我尝试嵌入来自Youtube、Vimeo和Dailymotion的不同视频。

不幸的是,目前除了我的容器视图的背景颜色之外,什么也没有显示出来:

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);

AVPlayer错误为空,我总是从状态中得到AVPlayerItemStatusUnknown的日志。有什么想法吗?
编辑1: 我将我的代码更改为:
@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];

很遗憾,我只能看到蓝色的 containerView :/

enter image description here

我认为问题不在 AVPlayer 本身,而可能是帧和层。


确保 AVPlayerLayer 的框架大小正确。 - onmyway133
实际上,你不能通过AVPlayer播放此链接http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player... 你需要该视频的直接链接... - Che
3个回答

10

你需要按照以下步骤进行:

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];

希望这能有所帮助。
补充说明:
这也取决于您的URL;如果像您所说的那样,您的URL格式如下:

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

然后你应该使用这个代替:
[NSURL urlWithString:item.url]; 

假设item是你的对象,url是该对象的一个属性,类型为NSString


@Davis 你尝试过更改背景颜色并查看是否完美加载,或者尝试调整帧数以查看是否在屏幕外显示吗?至少这样你会发现一些问题/缩小问题范围。如果确实是帧的问题,那么修复起来应该很容易,对吧? - Unheilig
请查看我的新编辑!是的,我使用了urlWithString,你可以看到。url是一个NSString,所以这应该是正确的!我还删除了item.url并直接在我的代码中编写了url,例如:AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:@"http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player"]]; - Davis
1
@Davis 当然,它可以工作-但仅支持AVFoundation支持的格式,从uTube播放肯定不起作用,必须使用uTube API才能播放音乐。但是尝试使用以.mp3结尾的不同链接进行测试,它会播放。虽然这不是一个令人满意的答案,但似乎这就是现实。顺便说一句,我自己运行了代码。希望这些信息仍然有所帮助。 - Unheilig
2
是的,看来我必须使用UIWebView来处理YouTube、Vimeo等内容。感谢您的帮助! - Davis
1
结果发现我无法让AVPlayer工作,因为我使用的是URLWithString而不是fileURLWithPath ‍♂️ 谢谢! - cargath
显示剩余5条评论

1

适合我的AVPlayer实现方式:

MP4:

player = [AVPlayer playerWithURL:videoPathUrl];
AVcontroller = [[AVPlayerViewController alloc] init];
[AVcontroller.view setFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.width)];
AVcontroller.player = player;
AVcontroller.showsPlaybackControls = FALSE;
[self addChildViewController:AVcontroller];
[self.view addSubview:AVcontroller.view];
[player play];

MP3 :

 playerItem = [AVPlayerItem playerItemWithURL:url];
    player = [AVPlayer playerWithPlayerItem:playerItem];
    player = [AVPlayer playerWithURL:url];
    [player play];

获取视频缩略图,试试这个。
AVAsset *asset = [AVAsset assetWithURL:videoPathUrl];
 AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
float tempTime = CMTimeGetSeconds(player.currentItem.duration);
CMTime time = CMTimeMake(tempTime, 1);  // (1,1)
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *aThumbnail = [UIImage imageWithCGImage:imageRef];

停止视频。
[player pause];
player = nil;

**为视频设置播放速率/速度**

[player play];
[player setRate:currentRate];

从头开始播放

[player seekToTime:kCMTimeZero];
 [player play];

用于检查视频是否正在播放

 if ((player.rate != 0) && (player.error == nil)) {
// playing

}

寻找视频(提前一段时间)
float tempSeekTime = CMTimeGetSeconds(player.currentItem.duration) + 10;
CMTime targetTime = CMTimeMakeWithSeconds(tempSeekTime, NSEC_PER_SEC);
[player seekToTime:targetTime];

0
使用PHImageManager的requestPlayerItemForVideo方法获取AVPlayerItem,这是播放AVAsset的最简单、最可靠的方法,可以完美、一致地执行。
我在这里使用它:

https://youtu.be/7QlaO7WxjGg


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接