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];
}
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;
}
}
I have a movie that loads fine and plays fine but it plays as soon as the page loads. I would like it to load in a paused state requiring the user to hen press play to watch.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController pause];
}
The pause on the last line does not seem to do anything. Any help? Thanks
Put your code blurb you've written above in viewwillappear.
Then add play at the end like so:
-(void) viewWillAppear: (BOOL) animated {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
}
at the end of it.
In a viewdidappear do a
-(void) viewDidAppear: (BOOL) animated {
[moviePlayerController pause];
}
Remember to remove code from viewDidLoad
You might want to try setting the MPMoviePlayerController property shouldAutoplay to NO.
Add the following line before your prepareToPlay call;
moviewPlayerController.shouldAutoplay = NO;
From the reference:
shouldAutoplay
A Boolean that indicates whether a movie should begin playback automatically.
#property (nonatomic) BOOL shouldAutoplay
Discussion
The default value of this property is YES. This property determines
whether the playback of network-based content begins automatically
when there is enough buffered data to ensure uninterrupted playback.
Availability
Available in iOS 3.2 and later.
Declared In
iOS 5.1 ,here is the code:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work
Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.
MPMovieSourceTypeStreaming is the thing you have missed.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
You may be sending your
[player play]
message too soon.
Use the following notification observation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
To listen for the loadState to change to playable. To check for that update do this :
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The movie should begin playing once it's ready.
Try this below code for play video from your project Resource :
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"map" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
Thanks..!
Building on #Dinesh's answer, your URL creation is incorrect. The value of
[NSURL URLWithString:#"map.mp4"]
will be nil since #"map.mp4" is not a valid URL. To create a valid url from the app's bundle do what Dinesh says. To create a remote URL you would do something like
[NSURL URLWithString:#"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]
The URL should contain all it's parts: protocol, host, path, file.
Cheers, Felipe
Add your movie player controller view on main windows like:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work!
Your player should be a property of your view controller, something like this:
.h:
#property(nonatomic,weak)MPMoviePlayerController *moviePlayer;
.m:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
//set the player to your property
self.moviePlayer=player;
[self.view addSubview: self.moviePlayer.view];
// ...
[self.moviePlayer play];
Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!
MPMoviePlayerViewController *moviePlayerViewController;
-(void)PlayVideoController:(NSString*)videoUrl1 {
NSURL *fileURL = [NSURL URLWithString:videoUrl1];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerViewController.moviePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}
You might want to cross check video url for blank spaces. Following code works for me.
- (IBAction)btnPlayVideo:(id)sender {
self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", self.strVideoUrl]];
NSLog(#"Url to play = %#", self.strVideoUrl);
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayerController.view];
self.moviePlayerController.shouldAutoplay = YES;
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.controlStyle=NO;
[self.moviePlayerController prepareToPlay];
self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
[self.moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
self.closeVideAdProp.hidden = NO;
btnCloseAd.hidden=FALSE;
}
Hi i have created app for ipad , it has an video files in resource folder and it plays when the user click play button.
It works for first time user clicks play button but when user plays subsequent times of video file.The issues occurs that is the video is not play only audio is playing.These issues occurs not randomly but some more times it will occurs.
One more thing at the time of issue occurs (Video is not playing ) , when i click the wo-arrow icons located at the bottom right corner of the player the movie goes to full screen and it shows video .At the time video is playing.
Can any one help me ?
Here is my sample code
MPMoviePlayerController *moviePlayer;
}
#property (readwrite, retain) MPMoviePlayerController *moviePlayer;
#synthesize moviePlayer;
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[super viewDidLoad];
}
-(IBAction)PlayBtnPressed:(id)sender
{
NSURL *movieURL;
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"title" ofType:#"mp4"];
movieURL = [NSURL fileURLWithPath:moviePath];
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// save the movie player object
self.moviePlayer = mp;
[mp release];
UIInterfaceOrientation orientation = [[UIDevice currentDevice]orientation];
[self shouldAutorotateToInterfaceOrientation:orientation];
// Play the movie!
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer play];
}
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
NSLog(#"movieFinishedCallback aNotification");
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player.view removeFromSuperview];
}
Thanks in advance ........
Looks like the movieplayer is not getting released until after you've allocated a second one on the same movie file:
self.moviePlayer = mp;
retains it, but the movie finished part doesn't do a
self.moviePlayer = nil;
Could be that's causing you problems.