Animation on AVPlayerViewController not synchronized - ios

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

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

iOS 9 AVPlayer rotation trouble

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!

How to get original image when we zoom in and zoom out the image in ios

Hi i am beginner in ios and in my project i am adding UIImage on UIScrollview and i have added tap gesture on UIImage
When we double click on UIImage then image should be zooming full screen size on view controller
After the full screen size image we can zoom it like any way what we want(i mean using like pinch zoom effect)here my requirement is when we double click on image then image need to set it's original position i have tried my level best but i did not get result please help me
my code is below:
#import "ViewController2.h"
#interface ViewController2 ()
{
UIScrollView * myScroll;
UITapGestureRecognizer *tap;
BOOL isFullScreen;
CGRect prevFrame;
UIImageView * _imageView;
}
#end
#implementation ViewController2
- (void)viewDidLoad
{
[super viewDidLoad];
isFullScreen = FALSE;
myScroll = [[UIScrollView alloc] init];
myScroll.frame = self.view.bounds;
myScroll.contentSize = CGSizeMake(_imageView.frame.size.width, _imageView.frame.size.height);
myScroll.maximumZoomScale = 4.0;
myScroll.minimumZoomScale = 1.0;
myScroll.clipsToBounds = YES;
myScroll.delegate = self;
myScroll.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myScroll];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_imageView setClipsToBounds:YES];
_imageView.userInteractionEnabled = YES;
_imageView.image = [UIImage imageNamed:#"ram.jpeg"];
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imgToFullScreen:)];
tapper.numberOfTapsRequired = 1;
[_imageView addGestureRecognizer:tapper];
[myScroll addSubview:_imageView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;
}
-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
prevFrame = _imageView.frame;
[_imageView setFrame:[[UIScreen mainScreen] bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[_imageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;
}];
return;
}
}
#end
I did some modifications in your code as like below try it .Check it give your wanted output or not.
#interface ScrollViewController ()<UIScrollViewDelegate>{
UIScrollView * myScroll;
UITapGestureRecognizer *tap;
BOOL isFullScreen;
CGRect prevFrame;
UIImageView * _imageView;
CGAffineTransform trans;
}
#end
#implementation ScrollViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
isFullScreen = FALSE;
myScroll = [[UIScrollView alloc] init];
myScroll.frame = [UIScreen mainScreen].bounds;
myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height);
myScroll.maximumZoomScale = 4.0;
myScroll.minimumZoomScale = 1.0;
myScroll.clipsToBounds = YES;
myScroll.delegate = self;
myScroll.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myScroll];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_imageView setClipsToBounds:YES];
_imageView.userInteractionEnabled = YES;
_imageView.image = [UIImage imageNamed:#"img.png"];
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imgToFullScreen:)];
tapper.numberOfTapsRequired = 2;
[_imageView addGestureRecognizer:tapper];
[myScroll addSubview:_imageView];
prevFrame = _imageView.frame;
trans = _imageView.transform;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;
}
-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
myScroll.contentSize = prevFrame.size;
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
_imageView.frame = [UIScreen mainScreen].bounds;
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
myScroll.contentSize = CGSizeMake(prevFrame.size.width, prevFrame.size.height);
_imageView.transform = trans;
_imageView.frame = prevFrame;
NSLog(#"%# %#",NSStringFromCGSize(myScroll.contentSize),NSStringFromCGRect(prevFrame));
}completion:^(BOOL finished){
isFullScreen = FALSE;
}];
return;
}
}

Play video in setFrame method

I have to play my video in view given CGRectMake. But always it's playing full screen only. I'm using MPMoviewPlayerViewController.
NSString *path;
path=[[NSBundle mainBundle] pathForResource:#"solar" ofType:#"mp4"];
mpviewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
mpviewController.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:mpviewController.view];
[self presentMoviePlayerViewControllerAnimated:mpviewController];
you have to set MpMoiveScalling mode to aspect fit so just set this line of code and you done.
MPMoviePlayerController *obj; [obj
setScalingMode:MPMovieScalingModeFill];
It worked for me.
Remove this line,
[self presentMoviePlayerViewControllerAnimated:mpviewController];
Working Sample code,
- (IBAction)playVideoAction:(id)sender
{
float width = 200;
float height = 300;
MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePathURL];
theMovie.view.frame = CGRectMake((self.view.frame.size.width - width)/2, (self.view.frame.size.height - height)/2, width, height);
[self.view addSubview:theMovie.view];
}
i apply this code in my application.
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
theMovie.view.frame = CGRectMake(0, 0, 200, 300);
Thanks.

MPMediaPlayerController turn hide user interface elements permanently

I using MPMediaPlayerController and i need hide elements of UI permanently.
But i can see it when player start play video, 2 seconds later it hide.
I using:
setControlStyle:MPMovieControlModeHidden.
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://catholictvhd-lh.akamaihd.net/i/ctvhd_1#88148/index_3_av-p.m3u8"]];
[player.view setFrame:self.view.bounds];
player.view.backgroundColor = [UIColor blueColor];
player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CGSize maxSize;
if([[UIScreen screens]count] > 1) {
[self logIt:#"External screen is available"];
newwindow = [[UIWindow alloc] init];
// There is a external display.
UIScreenMode *maxScreenMode;
for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
{
UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
if(current.size.width > maxSize.width)
{
maxSize = current.size;
maxScreenMode = current;
}
}
UIScreen *external = [[UIScreen screens] objectAtIndex:1];
external.currentMode = maxScreenMode;
[self logIt:[NSString stringWithFormat:#"%#",maxScreenMode]];
newwindow.screen = external;
// setting external display size.
CGPoint point = CGPointMake(0.0f, 0.0f);
//CGSize size = CGSizeMake(1024.0f, 768.0f);
CGSize size = maxSize;
CGRect frame = player.view.frame;
frame.origin = point;
frame.size.width = size.width;
frame.size.height = size.height;
[player.view setFrame:frame];
[player setControlStyle:MPMovieControlModeHidden];
[player setFullscreen:YES animated:YES];
player.shouldAutoplay = YES;
[player prepareToPlay];
[player play];
player.moviePlayer.controlStyle = MPMovieControlStyleNone; // to hide controls
Before playing the video you have to add subview as MPMoviePlayerController
Add this line before play
[self.view addSubview:player.view];

Resources