AVPLayer show black screen when screen load - ios

I am using AVPLayer to player video in UITableView. Video play properly but sometime initially when video played, sound is coming but screen in black. Video is visible after 5-6 second of video is played. I am using following code:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayer = avPlayer;
__weak CLBAVPlayer *weakSelf = self;
[self.avPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC)
queue:nil
usingBlock:^(CMTime time) {
[weakSelf progress];
}];
self.layer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Please help me to figure out the issue.

I got the answer for this. Actually the issue was that when I was fetching layer and adding it as sublayer then it was not on main queue. Using dispatch_async with the main queue solved my problem.

Related

AVPlayer Video play button crossed out

I am currently trying to retrieve an .mp4 from a URL.
The video is accessible from a web-browser but the play button is crossed out. Any ideas on what could be going wrong?
Code in question
NSURL *movieURL = [NSURL URLWithString:self.videoURL];
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:movieURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
// show the view controller
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.frame = self.view.frame;
Screenshot of device
I think there is no way apart from UIWebView to play youtube video inside iOS application. Also you can redirect to youtube application by passing your video url.So youtube application will handle rest of the things
You can read the guidelines of YouTube section 2 point 10 here
You can use youtube player here
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IMG_0597" ofType:#"mov"]];
AVURLAsset *asset = [AVURLAsset assetWithURL:movieURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem: item];
playerViewController.player = player;
[playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];
playerViewController.delegate=self;
playerViewController.showsPlaybackControls = YES;
[self.view addSubview:playerViewController.view];
[player play];
The reason why my videos did not play was because in the xml, where I was retrieving the url for the videos, I did not start the url with http://.
The code for this player works as it should.

Video is not showing me use AVPlayer only audio is working

I have made one sample demo
Play video on my simulator using AVPlayer.
But my problem is video is not showing where audio of that video is working.
Code is:
#property (nonatomic,strong)AVPlayerViewController *videoPlayer;
-(IBAction)playVideo:(id)sender {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"video" ofType:#"mp4"]];
AVPlayer *player = [AVPlayer playerWithURL:url];
self.videoPlayer = [[AVPlayerViewController alloc]init];
[self presentViewController:self.videoPlayer animated:YES completion:nil];
self.videoPlayer.player = player;
self.videoPlayer.showsPlaybackControls=YES;
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];
[player play];
}
Image
I guess that the problem is in your video file. Try to download another video mp4 video sample. Add it to your project and try.
You don't need this code:
self.videoPlayer.view.frame = self.view.frame;
[self.view addSubview:self.videoPlayer.view];

AVPlayer not playing video file in iOS

I am trying to play a video which is stored on my device using the following code. The problem is that I am seeing nothing on the screen, just a white screen.
The value of filePath in this case is file:///var/mobile/Containers/Data/Application/0E87EF09-B87C-443F-9BFB-E9A68AC4A162/Documents/TMRecordedVideos/2015-07-25T18-57-19Z.mov
NSString *filePath = [self.video fileURL];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];
AVAsset *videoAsset = [AVAsset assetWithURL:fileUrl];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:videoAsset];
self.videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
[self.videoPlayerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:self.videoPlayerLayer];
[self.videoPlayer play];
I am using AVPlayerLayer for playing video. I used the below code and it worked fine. The URL(in my case outputFileURL) is similar to your filePath.
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
Hope this may help you to sort out your issue.

What is the best way to play video in UITableViewCell

I'm trying to make auto-play video in UITableViewCell depending on cell position.
I'm using the AVPlayer
Here is my code:
__weak typeof(self)this = self;
NSString* videoPath = #"http://test.com/test.mp4";
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoPath] options:nil];
NSArray* keys = [NSArray arrayWithObjects:#"playable",nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(){
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
this.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
this.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:this.avPlayer];
this.avPlayerLayer.frame = _videoContent.frame;
dispatch_async(dispatch_get_main_queue(),^{
[this.videoContent.layer addSublayer:this.avPlayerLayer];
[this.avPlayer play];
});
}];
But my UITableView is frozen when i scroll the table.
I think there are many time-consuming work but most biggest thing is
[this.avPlayer play]
So my question is that AVPlayer is the best way in this situation?
And is there any way to improve the performance?
Are you sure that creating the AVPlayerItem, AVPlayer, and AVPlayerLayer can all be performed off the main thread? You might want to try putting those inside the block that dispatches on the main queue.
Use the below link , this suits for your question.
https://github.com/PRX/PRXPlayer

ios AVPlayer failed to stream remote video

I have a AVPlayer that plays video from remote url.
Here is my code:
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:videoUrl];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = playerView.bounds;
[playerView.layer addSublayer:playerLayer];
[self.player play];
When I start to stream I have only less then a second video chunck and then downloading stops and nothing happens.
MPMoviePlayerController and browser plays this video as usual.
I also doubt, that it might be effect of cropping video (because videos without cropping works fine). Here is the guide I use to crod video http://www.one-dreamer.com/cropping-video-square-like-vine-instagram-xcode/
Also clean app with same setup can't play video.
Any ideas? thanks!
Use the AVPlayerItemPlaybackStalledNotification
[[NSNotificationCenter defaultCenter] addObserverForName:AVPlayerItemPlaybackStalledNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
[weakself performBlockOnMainQueueAfterDelay:1.5 block:^{
[weakself.player play];
}];
}];

Resources