AVPlayer Full Screen - ios

i'm using AVPlayer in my apps instead MPMoviePlayerController since i want to put the player as sublayer.
However i need control like play, pause, search, and full screen. How do i get that button in AVPlayer?
This is how i put the player:
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:URL1 options:nil];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:avasset];
player = [[[AVPlayer alloc] initWithPlayerItem:item]retain];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
CGSize size = self.view.bounds.size;
float x = size.width/2.0-187.0;
float y = size.height/2.0 - 125.0;
playerLayer.frame = CGRectMake(x, y, 474, 320);
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:playerLayer];
[player play];

You'll have to provide your own buttons.
For play and pause there are direct API's in AVPlayer.
For full screen… well… you know where you are drawing: you just need to size your UIWindow and corresponding UIView & layer to be full size.
For search, you do have methods like seekToTime:

For future reference iOS 8 AVKit has the AVPlayerViewController class available that will wrap a AVPlayer and has the standard MPMoviePlayerController class controls.
AVPlayerViewController
AVPlayerViewController * filePlayerViewController = [AVPlayerViewController alloc];
//Show the controls
filePlayerViewController.showsPlaybackControls = true;
AVPlayer * filePlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:self.videoFile.localURL]];
filePlayerViewController.player = self.filePlayer;

Related

AVPlayer not showing Done button in objective c

I am trying to show live streaming in bottom left corner of my screen,Currently live video is playing smoothly with all controls(Play, pause ,volume,full screen).But close/done button is not there.so I am not able to close live streaming window at any time I want to close it.I am using AVPlayer to achive same .Below is my code.
I have tried MPPlayer also but had same problem with that.
I had taken one close button and tried playerlayer.removefromsuperlayer but this also not worked for me.
I just want one close button which will close live streaming from my app
Please suggest any another player if they support close functionality
NSURL *videourl = [NSURL URLWithString:#"my url"];
AVPlayer *player = [AVPlayer playerWithURL:videourl];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
playerViewController.view.frame = CGRectMake(0, 400, 300, 200);
AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = CGRectMake(0, 400, 300, 200);
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
//externalPlaybackVideoGravity
playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
playerViewController.showsPlaybackControls = YES;
[self addChildViewController:playerViewController];
[self.view.layer addSublayer:playerLayer];
[self.view addSubview:playerViewController.view];
playerViewController.player.muted = TRUE;
[playerViewController.player play];

How to add the UIButton present in storyboard to the top of AVPlayer in iOS?

I am doing the implementation of video splash similar to Uber in my application. I had added two buttons in the storyboard at the bottom part with equal spacing in storyboard. Now i am adding AVPlayer to play a video present in NSBundle. While running the application the video is playing but i cant see my buttons at the bottom part. Why it is happening? Please suggest..
- (void)createVideoPlayer {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"welcome_video" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:filePath];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player.volume = PLAYER_VOLUME;
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.videoGravity = UIViewContentModeScaleToFill;
playerLayer.frame = self.playerView.layer.bounds;
[self.playerView.layer addSublayer:playerLayer];
[self.player play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}
- (void)moviePlayDidEnd:(NSNotification*)notification{
AVPlayerItem *item = [notification object];
[item seekToTime:kCMTimeZero];
[self.player play];
}
The screenshot of buttons in storyboard:
The output in simulator:
You can see buttons are missing and Note: constraints of these buttons are correct and i can see them in every preview...
You need to call bringSubviewToFront to bring your Button outlet front and visible on AVPlayer after adding AVPlyer instance in your view.
[self.view bringSubviewToFront:buttonOutlet];
You can try calling bringSubviewToFront on the button after you've added the avplayer.

Best way to play video from Url in iOS

I am playing a video from Url, It is playing but very slow. I am using my mobile data.
Her is the code I play the video:
self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:_selectedVideo.videoLink]];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[self.avPlayer play];
It is really slow and takes long time to start. Is there any way to make it faster?
Thanks
You can try mpmovieplayer to play video.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame
[myView addSubview: player.view];
// ...
[player play];

Objective-C How to play videos on MPMoviePlayerController or AVPlayer instantly, without delay?

I have tried playing 500 KB videos on AVPlayer and MPMoviePlayerController, and they both have a slightly annoying delay. AVPlayer and MPMoviePlayerController both display a black screen for 2-3 seconds, and then it starts the video. Thats really annoying, how come it takes so long for the video to start? How can I fix it?
//Movie Player Code
_moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:video];
_moviePlayerVC.moviePlayer.view.userInteractionEnabled = YES;
_moviePlayerVC.moviePlayer.view.multipleTouchEnabled = YES;
_moviePlayerVC.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayerVC.moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayerVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
_moviePlayerVC.moviePlayer.view.frame = self.frame;
[_moviePlayerVC.moviePlayer setShouldAutoplay:YES];
_moviePlayerVC.view.frame = self.frame;
[self addSubview:_moviePlayerVC.view];
//AVPlayer Code
AVAsset *asset = [AVAsset assetWithURL:video];
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
_player = [[AVPlayer alloc] initWithPlayerItem:item];
_player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
layer.frame = self.frame;
layer.backgroundColor = [UIColor clearColor].CGColor;
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
You could preload an AVPlayer and observe its status. Once it changes AVPlayerStatusReadyToPlay you should be able to start playback without any delay.

Synchronize video to openGL animation

I am trying to sync a video to an animation drawn using openGL on iPad, and there are two things I am not sure how to do:
Find the currently playing video frame.
Make sure the update of the video and update of the openGL drawing occurs at the exact same time, as even a slight sync issue may cause visual artifacts.
Not sure it will work but instead of using MPMoviePlayerController you could use an AVPlayer and use and AVSynchronizedLayer to synchronize your OpenGL layer timing with the AVPlayerLayer's one.
You could create the video player like this:
NSURL *videoURL = [NSURL fileURLWithPath:#"your_url"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
UIView *playerView = [[UIView alloc] initWithFrame:frame];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
[playerLayer setFrame:[[playerView layer] bounds]];
Then create a AVSynchronizedLayer layer and sync it with the playerLayer:
AVSynchronizedLayer *syncLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:playerItem];
[syncLayer addSublayer:yourGlView.layer];
[playerView.layer addSublayer:playerLayer];
[playerView.layer addSublayer:syncLayer];
[self.view addSubview:playerView];
...

Resources