iOS 9 AVPlayer rotation trouble - ios

I have some strange thing - after rotating of AVPlayer, I'm updating textField and slider of progress by timer, but view with controllers change coordinates after AVPlayer
mPlayer = [[AVPlayer alloc] init];
mPlayer = [mPlayer initWithURL:streamURL];
playerLayer = [AVPlayerLayer playerLayerWithPlayer:mPlayer];
[[[self view] layer] insertSublayer:playerLayer atIndex:0];
[playerLayer setFrame:self.view.bounds];
rotating
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.ButtonsView.frame.size.height);
if([[UIDevice currentDevice]orientation]==UIDeviceOrientationLandscapeLeft||[[UIDevice currentDevice]orientation]==UIDeviceOrientationLandscapeRight)
{
self.ButtonsView.frame = CGRectMake((self.view.frame.size.width - self.ButtonsView.frame.size.width) /2, playerLayer.frame.size.height, self.view.frame.size.width, self.ButtonsView.frame.size.height);
}
}
and after change of timer, coordinates of ButtonView became wrong
- (void)updateTime:(NSTimer *)timer {
self.tfCurrent.text = [self textFormatting : self.videoCurrentTime];
}
first second coords are good. Why my View redraw? thanks!

Related

AVPlayer and Uiview animation issue

I have a view controller with 3 components:
1) Tableview
2) UIView (on Tableview)
3) AVPlayerLayer inside UIView
When I scroll tableview I want a resize of UIView and its layer (AVPlayerLayer) and and this I managed to do it.
My problem is that the resize of the view happens in a non-synchronous way with that of the layer while I would like that while the view gets smaller even the layer would follow the shrinking of the view itself.
Here is a video that shows the behavior I get: https://streamable.com/s/z4yc2/eakmvv
This is the viewcontroller's code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.lastContentOffset = 0;
[self setupPlayer];
[self.table reloadData];
}
-(void)setupPlayer
{
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:#"testurlvideostreaming"]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer * avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
self.avPlayerLayer.frame = CGRectMake(0, 0, self.viewStreaming.frame.size.width, self.viewStreaming.frame.size.height);
[self.avPlayerLayer setBackgroundColor:[UIColor grayColor].CGColor];
[self.viewStreaming.layer addSublayer:self.avPlayerLayer];
[avPlayer play];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound &&
indexPath.row >= 5 &&
self.lastContentOffset < tableView.contentOffset.y)
{
float percentage = .7f;
int width = 50;
int height = width;
int xPosition = self.viewStreaming.superview.frame.size.width - width;
int yPosition = self.viewStreaming.superview.frame.size.height * ((1 - percentage) / 2);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.viewStreaming.frame = CGRectMake(xPosition, yPosition, width, height);
self.avPlayerLayer.frame = CGRectMake(0, 0, self.viewStreaming.frame.size.width, self.viewStreaming.frame.size.height);
[UIView commitAnimations];
}
}

Animation on AVPlayerViewController not synchronized

I have a AVPlayerViewController that I create in this way:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *videoURL = [NSURL URLWithString:#"testURL"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[playerViewController.player play];
[playerViewController.view setFrame:CGRectMake(0, 0, 300, 300)];
[self.view addSubview:playerViewController.view];
[self addChildViewController:playerViewController];
int width = 100;
int height = 150;
int xPosition = self.view.frame.size.width - width;
int yPosition = 100;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
playerViewController.view.frame = CGRectMake(xPosition, yPosition, width, height);
[UIView commitAnimations];
}
My problem is that in this way I get this:
the playerViewController.view's animation is not synchronized with the
video and the playerViewController.view slowly reduces while the video
is immediately reduced.
I don't understand why.
if you need it I can link a video of what I get

Remove contant of a previous page when next load in collection view in paging

I am using a UICollectionView with paging feature and adding AVPlayer on each cell to play video with URL, but my collection view is giving a strange behavior. When it loads the first cell and video starts playing then it is fine but when I slide and load the next cell then both videos are playing together, while I want the previous one to stop playing. I tried many thing to resolve this issue but nothing is working, someone please suggest me something to work with this. My code of custom cell class where I am adding AVPlayer is given below:
[self.avPlayer pause];
self.avPlayer = nil;
[self.avLPlayer removeFromSuperlayer];
CGRect videoRect;
videoRect = CGRectMake(0,0,[[UIScreen mainScreen]bounds].size.width,[[UIScreen mainScreen]bounds].size.height);
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
[indicator bringSubviewToFront:_viewForShowVideo];
UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
indicator.frame = CGRectMake(currentWindow.frame.size.width/2, _viewForShowVideo.frame.size.height/2, 20, 20);
[_viewForShowVideo addSubview:indicator];
self.avPlayerItem = [AVPlayerItem playerItemWithURL:Url];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
self.avLPlayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
[_avLPlayer setFrame:videoRect];
[_avLPlayer setBackgroundColor:[UIColor blackColor].CGColor] ;
[_avLPlayer setVideoGravity:AVLayerVideoGravityResizeAspect];
_avLPlayer.opacity = 1;
_avPlayer.allowsExternalPlayback = NO;
[_avPlayer setMuted:NO];
_avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[AVAudioSession sharedInstance]setCategory: AVAudioSessionCategoryPlayback error: nil];
[_viewForShowVideo.layer addSublayer:_avLPlayer];
[_avPlayer play];
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnPlayer:)];
[_viewForShowVideo addGestureRecognizer:tgr];
[_viewForShowVideo setBackgroundColor:[UIColor greenColor]];

Play the video in Landscape mode iOS 7

In my application, I'm trying to play video using URL from my server . I'm using the UITableView to display the video list and by tapping the Cell from the list, the video will play in sub view. Now I want to play the video in landscape mode.
This is the current video code.
_movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
[[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: [_movieplayer view]];
[_movieplayer setShouldAutoplay:YES];
[_movieplayer prepareToPlay];
[self.movieplayer play];
In the above code, how do I make it work so that it plays in landscape mode. Please guide me on this. Been stuck on this for a long time.
Add this to your viewcontroller with the movieplayer
#property (nonatomic, strong) MPMoviePlayerController* mpc;
- (void)setUpMPC
{
NSURL* m = [[NSBundle mainBundle] URLForResource:#"YourVideo" withExtension:#"mp4"];
MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(50, 50, self.view.bounds.size.width, self.view.bounds.size.height);
}
-(NSUInteger)supportedInterfaceOrientations {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskLandscape;
}
Probably you should implement the shouldAutorotateToInterfaceOrientation and supportedInterfaceOrientations methods in your viewController
#property (nonatomic, strong) MPMoviePlayerController* moviePlayerController;
#pragma mark # - Init -
- (void) createAndPlayMovieForURL: (NSURL*) movieURL
{
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[self.moviePlayerController.view setFrame: self.view.bounds];
[self.view addSubview: self.moviePlayerController.view];
[self.view bringSubviewToFront: self.overlayView];
}
#pragma mark - Rotation -
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
- (NSUInteger) supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}

Got the message "WARNING: under normal conditions, _fillInQueueWithExtraSpace:.." and MPMoviePlayer rotation not work in iPad IOS 5.1

It's my first post, and may be it may seem incorrect.
So, I've to make rotation in cocos2d on iPad (5.1)
I use 2 different videos to each orientation.
And there I have 2 problems:
The app starts in portrait mode, and plays video normally. I call (play) the video 5-10 times, when video finishs I rotate simulator. The view rotates, BUT when I call (play) video - it shows white screen and the next message:
"WARNING: under normal conditions, _fillInQueueWithExtraSpace:ignoreExistingItems: should not be re-entered."
Then If I rotate screen again (several times) - and play it in landscape mode - it plays video well.
Also vice versa. When I start from landscape mode
The View rotating problem.
When I rotate view to the left/right landscape (from portrait) - can't rotate view backward. So I can rotate only in clockwise or counter clockwise. How to fix it?
-(id) init {
pathToVideoP = [[NSBundle mainBundle] pathForResource:#"video_portrait" ofType:#"mp4"];
pathToVideoL = [[NSBundle mainBundle] pathForResource:#"video_landscape" ofType:#"mp4"];
theMovieP = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:pathToVideoP]];
theMovieL = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:pathToVideoL]];
}
-(void) playVideoButtonClicked {
movieButton.visible = FALSE;
if (sharedManager.isPortrait){
theMovie = theMovieP;
} else {
theMovie = theMovieL;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[theMovie moviePlayer]];
CGSize size = [[CCDirector sharedDirector] winSize];
[[[CCDirector sharedDirector] openGLView] addSubview:theMovie.view];
player = [self.theMovie moviePlayer];
player.controlStyle = MPMovieControlStyleNone;
[theMovie moviePlayer].view.backgroundColor = [UIColor whiteColor];
theMovie.view.frame = CGRectMake(0, 0, size.width, size.height);
if (sharedManager.isPortrait) {
CGAffineTransform transform = player.view.transform;
player.view.transform = transform;
}
else if (sharedManager.changeOrientation)
{
CGAffineTransform transform = player.view.transform;
transform = CGAffineTransformRotate(transform, (-M_PI/2 ));
player.view.transform = transform;
}
sharedManager.changeOrientation = NO;
player.backgroundView.backgroundColor = [UIColor whiteColor];
theMovie.view.backgroundColor = [UIColor whiteColor];
player.view.userInteractionEnabled = NO;
player.scalingMode = MPMovieScalingModeNone;
[player play];
}
-(void) moviePreloadDidFinish:(id)sender {
}
-(void) movieFinishedCallback:(NSNotification*) aNotification {
theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[player stop];
[theMovie.view removeFromSuperview];
movieButton.visible = TRUE;
}
Add this line of code After creating Player Object.
player = [self.theMovie moviePlayer];
player.controlStyle = MPMovieControlStyleNone;
It's necessary in below version of iOS 6.0. May be it's helpful.
[player prepareToPlay];

Resources