AVPlayer doesn't show anything
Asked Answered
S

3

8

I try to embed different videos from youtube vimeo, dailymotion.

Sadly at the Moment nothing is shown except the backgroundcolor of my 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 is Null and the only Log i always get from the Status is: AVPlayerItemStatusUnknown. Any ideas?

EDIT 1:

Ich changed my Code to:

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

Sadly the only thing i can see is the blue containerView :/

enter image description here

I think the Problem is not the AVPlayer himself, but the frames and the layer maybe....

Signification answered 3/3, 2014 at 14:55 Comment(2)
Make sure AVPlayerLayer frame is sized correctlySchear
Actually, you can't play youtube.com/… this link via AVPlayer… You need direct link for the video…Nightgown
N
10

You'd have to do the following:

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

Hope this helps.

Addendum:

It also depends on your URL; if as you said, you have one such as in this format:

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

Then you should use this instead:

[NSURL urlWithString:item.url]; 

Given that item is your object and url is a property there of object type NSString.

Nineteenth answered 3/3, 2014 at 18:47 Comment(10)
Thank you very much for your response, i edit my answer, sadly it still dont works :/Signification
@Signification Have you then tried changing the background color and see if it did get loaded perfectly or play around with the frame numbers and see if it was somehow get shown offscreen? At least then you would find out something / narrow down the problem. If it is in fact a frame problem, then it would be an easy fix, right?Nineteenth
I tried a lot with changing the frames and more, but it seems everything works perfectly. So i think the only Problem could be the URL...i also tried to use other youtube videos, but nothing worked....Signification
@Signification I supposed you tried with urlWithString? Btw, could you show item and url? What we could try further is try adding a video to your project and play it there instead using fileURLWithPath:, if that works, we'd know that the player is loaded fine and the problem would be narrowed down to - the url.Nineteenth
Please see my new Edit! Yes i used urlWithString as you can see. url is a NSString, so this should be correct! I also delete the item.url and wrote the url directly in my code, like: AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:@"youtube.com/… "]];Signification
@Signification Sure, it does work - but only with the formats supported by AVFoundation and certainly playing from uTube doesn't work and one has to use the uTube API to play music from it. But try it with a different link ending with format such as .mp3, it plays. Not a satisfactory answer, but it seems to be the reality. I ran the code myself, btw. Hope this information, still, helps.Nineteenth
Yep, it seems i have to use a UIWebView for youtube vimeo and Co., thanks for your help!Signification
@Signification do you if <MediaPlayer/MediaPlayer.h> frame work can help to play youtube videos ?Jeramey
@Nineteenth - same problem I hear audio, video is playing but I am not seeing any video just black window; I'm writing an application for MacOS using Swift 4 Xcode 9. Here is total code: let fileURL = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4") let avAsset = AVURLAsset(url: fileURL!, options: nil) let playerItem = AVPlayerItem(asset: avAsset) videoPlayer = AVPlayer(playerItem: playerItem) let playerView = self.playerView playerView?.player = videoPlayer videoPlayer.play() Any ideas?Roxieroxine
Turns out i couldn't get AVPlayer to work, because i was using URLWithString instead of fileURLWithPath 🤦‍♂️ Thanks!Bartell
U
1

AVPlayer Implementation which working for me:

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

For getting thumbnail form video try this

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

For Stop Video

[player pause];
player = nil;

**Setting Play rate/Speed for video **

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

Play From start

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

for checking video is playing or not

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

}

For seeking video (for some duration ahead)

float tempSeekTime = CMTimeGetSeconds(player.currentItem.duration) + 10;
CMTime targetTime = CMTimeMakeWithSeconds(tempSeekTime, NSEC_PER_SEC);
[player seekToTime:targetTime];
Unfamiliar answered 17/10, 2019 at 10:14 Comment(0)
K
0

Use the requestPlayerItemForVideo method of PHImageManager to acquire an AVPlayerItem; it is the simplest, sure-fire way to play an AVAsset, performing flawlessly and consistently.

I use it here:

https://youtu.be/7QlaO7WxjGg

Kassa answered 4/8, 2016 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.