Creating an AVPlayer for video - ios

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;

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 does not show Video Screen

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.

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

Video not play in Objective c

I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];

How to hide AVPlayerViewController control bar?

I've implemented iAd's Preroll Video Ad and I want to guarantee that my user will watch the entire advertisement. How do I hide the AVPlayerViewController's control bar so the user can not tap "Done" and get out of the video before it finishes?
self.canDisplayBannerAds = YES;
[AVPlayerViewController preparePrerollAds];
player = [[AVPlayerViewController alloc] init];
player.showsPlaybackControls = NO;
player.delegate = self;
Simple code
// create an AVPlayer
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.showsPlaybackControls = FALSE;
You can use this code to do the same. You need to call play when you present the controller.
playerItem = [[AVPlayerItem alloc] initWithURL:url];
if(playerItem) {
player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = _player;
[playerViewController setShowsPlaybackControls:NO];
[parentViewController presentViewController:playerViewController animated:YES completion:^{
[playerViewController.player play];
}];

Resources