AVPlayer does not show Video Screen - ios

I want to play a video using AVPlayer.Problem is video screen does not appear but sound come. It show black screen only.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
//[prefs URLForKey:#"marge_video_url"];
AVAsset *asset = [AVAsset assetWithURL:[prefs URLForKey:#"marge_video_url"]];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:item];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[self.VideoLayerView.layer addSublayer:self.playerLayer];
[self.player play];
self.videoPlaybackPosition = 0;
}
- (void)viewDidLayoutSubviews {
self.playerLayer.frame = CGRectMake(0, 0, self.VideoView.frame.size.width, 250);
}
This same code i used another controller and can play video nicely. But here this code not working.

Related

AVQueuePlayer doesn’t play wav file smoothly from remote URL. It is choppy and stops in the middle

Here is code for initialise AVQueuePlayer.
AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] init];
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
if ([queuePlayer respondsToSelector:#selector(automaticallyWaitsToMinimizeStalling)])
queuePlayer.automaticallyWaitsToMinimizeStalling = NO;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
if (playerItem)
[queuePlayer insertItem:playerItem afterItem:nil];
if ([queuePlayer respondsToSelector:#selector(playImmediatelyAtRate:)])
[queuePlayer playImmediatelyAtRate:1.0];
else [queuePlayer play];

AVPlayer not showing FullScreen

I am working on playing a recorded video recorded by AVCapture.I am saving the video URL in string named outputFileURL. I tried playing back the video using AVPlayerLayer concept. The code I used is
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
But the video I am getting is not full screen.
Can anyone can help me to solve?
I added the following code and I am able to get the full screen.
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
Hope this may help.
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.delegate = self;
playerViewController.player = player;
[playerViewController.player play];
[self presentViewController:playerViewController animated:YES completion:nil];
Try This
Ok than you have to use MPMoviePlayerController
NSString *path = [[NSBundle mainBundle] pathForResource:#"Video_Intro" ofType:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
moviePlayer.view.frame = ivVideoThumbnail.frame;
//moviePlayer.view.top += 20; // Add to fix 20 pixel diff of moviePlayer view
// Register this class as an observer instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[svIntro addSubview:moviePlayer.view];
moviePlayer.fullscreen = true;
[moviePlayer play];
Also implement Observer method
-(void)movieFinishedCallback:(id)mPlayer
{
[moviePlayer.view removeFromSuperview];
}

Not able to play video file on iPad using AVPlayerViewController

I have seen exactly the same issue here but its unanswered so asking the same question again.
AVPlayer wont play video from url in iOS9
I tried all various ways to try to play the video but unfortunately they are not working for me.
Here is the code :
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
NSString *url = [[NSBundle mainBundle] pathForResource:#"Intro" ofType:#"mp4"];
_asset = [AVURLAsset assetWithURL: [NSURL URLWithString:url]];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
_player = [[AVPlayer alloc] initWithPlayerItem: _playerItem];
[playerViewController.view setFrame:_videoView.frame];
playerViewController.showsPlaybackControls = YES;
[_videoView addSubview:playerViewController.view];
playerViewController.player = _player;
[_player play];
and here is the screen shot of what it looks like currently by using above code. And I used another Apple code to check the video and it worked fine but the Apple code is like too much for me as its too complicated for simple things. Here it is :https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationSimplePlayer-iOS/
Thanks
AJ
I was finally able to figure this out by again looking into Apple's Sample code.
Solution:
Added the AAPLPlayerView files to my project.
Then changed my UIView in Storyboard to above class
Then updated below lines of code into my view controller class:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.playerView.playerLayer.player = self.player;
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:#"Intro" withExtension:#"mp4"];
self.asset = [AVURLAsset assetWithURL:movieURL];
self.playerItem = [AVPlayerItem playerItemWithAsset: self.asset];
[self.player play];
}
- (AVPlayer *)player {
if (!_player)
_player = [[AVPlayer alloc] init];
return _player;
}
- (AVPlayerLayer *)playerLayer {
return self.playerView.playerLayer;
}
- (AVPlayerItem *)playerItem {
return _playerItem;
}
- (void)setPlayerItem:(AVPlayerItem *)newPlayerItem {
if (_playerItem != newPlayerItem) {
_playerItem = newPlayerItem;
// If needed, configure player item here before associating it with a player
// (example: adding outputs, setting text style rules, selecting media options)
[self.player replaceCurrentItemWithPlayerItem:_playerItem];
}
}
And it worked.
The issue was the PlayerLayer was not set properly and the video was not visible due to that.

AVPlayer plays audio but video freezes

I have two UIViewControllers each with an AVPlayer that are supposed to play a video file when they are pushed into a UINavigationController:
-(void)viewWillAppear:(BOOL)animated{
videoView = [[UIView alloc]initWithFrame:self.view.frame];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *foregroundLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
foregroundLayer.frame = self.view.frame;
foregroundLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[videoView.layer addSublayer:foregroundLayer];
[self.view addSubview:videoView];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
}
-(void)viewDidAppear:(BOOL)animated{
[self.avPlayer play];
}
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.navigationController popViewControllerAnimated:NO]
}
The first time I push any of the UIViewControllers the playback works well. But after that, if I push any of them again the sound plays but the video freezes.
I've tried using a MPMoviePlayerViewController but the behavior is the same. Any thoughts?

Creating an AVPlayer for video

I am creating an AVPlayer to display a video. I have some code to create the Player but nothing displays. I'm not sure if the URL is not working or if the player it's self is not working.
Here is the code I am using to create the player:
self.playing = NO;
AVPlayer *player = [[AVPlayer alloc] init];
player = [player initWithURL:self.videoURL];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[[[self view] layer] insertSublayer:playerLayer atIndex:3];
[playerLayer setFrame:self.view.bounds];
[player play];
Here is the code to save the video:
self.videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"%#", self.videoURL);
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.videoURL = self.videoURL;

Resources