Trato de incrustar diferentes videos de youtube vimeo, dailymotion.
Tristemente en el Momento no se muestra nada, excepto el color de background de mi containerView:
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 Error es nulo y el único logging que siempre obtengo del estado es: AVPlayerItemStatusUnknown. ¿Algunas ideas?
EDIT 1:
Ich cambió mi código a:
@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];
Lamentablemente, lo único que puedo ver es el blue containerView: /
Creo que el problema no es el propio AVPlayer, pero los frameworks y la capa tal vez …
Tendrías que hacer lo siguiente:
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];
Espero que esto ayude.
Apéndice:
También depende de su URL; si como dijiste, tienes uno tal como en este formatting:
http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player
Entonces deberías usar esto en su lugar:
[NSURL urlWithString:item.url];
Dado que el item
es su object y url
es una propiedad allí del tipo de object NSString
.
Utilice el método requestPlayerItemForVideo de PHImageManager para adquirir un AVPlayerItem; Es la forma más simple y segura de jugar un AVAsset, que se realiza de manera impecable y consistente.
Lo uso aquí: