Stretch video on AVPlayerLayer - ios

I am using the following code to show a full-screen video on my UIView:
- (void)playMovie:(NSString *)name :(NSString *)type
{
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];
self.movieAsset = [AVAsset assetWithURL:movieURL];
self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];
self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.movieLayer.videoGravity = AVLayerVideoGravityResize;
self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[self.movieLayer setFrame:self.view.frame];
[self.view.layer addSublayer:self.movieLayer];
[self.moviePlayer addObserver:self forKeyPath:#"status" options:0 context:nil];
// Schedule stop after 6 seconds
[NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}
The video is playing, but it doesn't fill (stretch if needed) the entire screen, but it only resizes maintaining its width/height ratio: I have tried all the three values of "videoGravity"... nothing seems to change.
How can I solve?
Thank you

Your setting the Gravity then re-Initing the player try:
- (void)playMovie:(NSString *)name :(NSString *)type
{
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:type]];
self.movieAsset = [AVAsset assetWithURL:movieURL];
self.movieItem = [[AVPlayerItem alloc] initWithAsset:self.movieAsset];
self.moviePlayer = [[AVPlayer alloc] initWithPlayerItem:self.movieItem];
self.movieLayer = [AVPlayerLayer playerLayerWithPlayer:self.moviePlayer];
[self.movieLayer setFrame:self.view.frame];
self.moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
self.movieLayer.videoGravity = AVLayerVideoGravityResize;
[self.view.layer addSublayer:self.movieLayer];
[self.moviePlayer addObserver:self forKeyPath:#"status" options:0 context:nil];
// Schedule stop after 6 seconds
[NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(stopCurrentMovie:) userInfo:nil repeats:NO];
}

Related

How to play the Video in iphone

NSURL *url="file:///Users/adi/Library/Developer/CoreSimulator/Devices/F7C9D729-6E67-45C3-9234-629460FDD0A4/data/Containers/Data/Application/319F0627-CF3E-43D4-A44F-3E5E797CCCEA/Library/Caches/Attachments/7157783VID_20150602_185133.mp4"
Here's my code:
MPMoviePlayerController * moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(10, 100, 300, 200);
[moviePlayer play];
It shows blank screen.
Hope this may help you
#import<MediaPlayer/MediaPlayer.h>
-(IBAction)VideoPlayer:(id)sender
{
NSString *path = [[NSBundle mainBundle]pathForResource: #"video-en-2b" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerViewControlle alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
Codin[self presentModalViewController:moviePlayer animated:NO];
}
Well if you have to play video using local file use this code.It is helpful for you.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"mp4"];//7157783VID_20150602_185133 name
NSURL*theurl=[NSURL fileURLWithPath:thePath];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(10, 100, 200, 300)];
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:moviePlayer.view];
If you want to control playback process
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
Try this :
-(void)playVideo : (NSString*)apath
{
NSURL *videoURL =[NSURL fileURLWithPath:apath];
if ([[NSFileManager defaultManager] fileExistsAtPath:apath]) //Does file exist?
{
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
}
Note: this method only accept local file path

MPMoviePlayerViewController -- how to eliminate black flash when video loads?

I'm using MPMoviePlayerViewController to show a video in my app. It works! Only problem is that there's a black flash just before the movie plays.
How can I get rid of the black flash? I've seen other threads, but they don't seem to have an explanation that works with MPMoviePlayerViewController and is sufficiently specific/detailed for a novice like me (most are for MPMoviePlayerController).
Would really appreciate any help!
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] init];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpvc.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mpvc.moviePlayer setContentURL:fileURL];
[mpvc.moviePlayer play];
[self presentViewController:mpvc animated:NO completion:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpvc.moviePlayer];
After endlessly iterating and tweaking, I stumbled my way into a solution using MPMoviePlayerController.
Don't forget to declare the property in the .h file, e.g.
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
Then
// Add default image to smooth transition
UIImage *myImage = [UIImage imageNamed:#"aiw_launch1136_black.png"];
self.videoStartFrame.image = myImage;
// Play the intro video
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"aiw_intro_video" ofType:#"mp4"]]];
self.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
[self.moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayer.view];
self.moviePlayer.view.hidden = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(isMovieReady:) name:MPMoviePlayerLoadStateDidChangeNotification object:self.moviePlayer];
// Detect that the video is ready and unhide the view
-(void)isMovieReady:(NSNotification *)notification {
MPMoviePlayerController *moviePlayer = [notification object];
if(moviePlayer.loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))
{
self.moviePlayer.view.hidden = NO;
}
}

How to init MPMoviePlayerController with video paused

I'm developing an app for iOS where it shows a movie, my method is a class method this is the code
Functions.m
static MPMoviePlayerController *moviePlayer = nil;
+(void)playMP4Movie:(NSString *)moviename
insideOfView:(UIView *)view
autoplay:(BOOL)autoplay{
NSString *path = [[NSBundle mainBundle] pathForResource:moviename ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
moviePlayer.view.frame = view.bounds;
[moviePlayer prepareToPlay];
if (autoplay) { [moviePlayer play]; }
else { [moviePlayer stop]; }
[view addSubview:moviePlayer.view];
}
and where I want to call it with video paused:
[Functions playMP4Movie:videoName insideOfView:_videoPlayerView autoplay:NO];
it shows my video but no matter if I set autoplay to YES or NO, the result is always the same... any help I'll appreciate
Try using:
if (autoplay) { [moviePlayer setShouldAutoplay:YES]; }
else { [moviePlayer setShouldAutoplay:NO]; }
And see if you have better luck.

Looping video using MPMoviePlayerController somewhere between startTime and endTime of the Video

Suppose the video duration is 50 seconds. In need to loop part of the video between 10th sec to 20th sec.
Very first time when I play the video it works fine. But for 2nd iteration onwards the video plays from the start (i.e from 0th second rather than 10th second).
Following is my code:
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:currentVideoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.moviePlayer.view setFrame: movieView.bounds];
[self.moviePlayer.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.moviePlayer setInitialPlaybackTime:10.0];
[self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer prepareToPlay];
self.moviePlayer.shouldAutoplay = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
[self.movieView addSubview:moviePlayer.view];
- (void)moviePlayerDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
// [self.moviePlayer setInitialPlaybackTime:10.0];
// [self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer play];
}
}
}
Also I tried to reset the InitialPlaybackTime and EndPlaybackTime in by moviePlayerDidFinish Method, but didn't worked.
Depending on the requirement I only need to loop only particular part of the video.
Please help me.. Thanks!!!
set the property repeatMode = MPMovieRepeatModeOne of MPMoviePlayerController self.moviePlayer.
self.moviePlayer repeatMode = MPMovieRepeatModeOne;
See Example
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"1" ofType:#"MOV"]];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.initialPlaybackTime = 3.0;
_moviePlayer.endPlaybackTime = 5.0;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

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?

Resources