I have a view within my view controller and have it declared as a property _movieSubview. I add a MPMoviePlayerViewController to _movieSubview and the video plays nicely.
-(void)moviePlay{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"High Rope" ofType:#"mov"]];
_playerController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
//[self presentMoviePlayerViewControllerAnimated:_playerController];
_playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[_playerController.moviePlayer prepareToPlay];
_playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
_playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
_playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_playerController.moviePlayer play];
/*[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_playerController];*/
//---play partial screen---
_playerController.view.frame = CGRectMake(0, 0, 320, 460);
[_movieSubview addSubview:_playerController.view];
}
- (void)moviePlayerDidFinish:(NSNotification *)note
{
if (note.object == _playerController) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[_playerController.moviePlayer play];
}
}
}
My 2 questions are:
My status bar disappears when I add the MPMoviePlayerViewController inside my subview. How do I keep the status bar?
How can I have the video play twice on repeat, then wait for user to play after that?
You should present _playerController instead of adding as subview. Use this code while creating the instance of MPMoviePlayer:
[moviePlayer setFullscreen:YES animated:YES];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:#selector(moviePlayerEvent:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
[notificationCenter addObserver:self selector:#selector(playBackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
-(void)moviePlayerEvent:(NSNotification*)aNotification{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}
-(void)playBackStateChanged:(id)sender{
MPMoviePlaybackState playbackState = [moviePlayerViewController.moviePlayer playbackState];
switch (playbackState) {
case MPMoviePlaybackStateStopped :
//play again
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
break;
case MPMoviePlaybackStatePlaying :
break;
case MPMoviePlaybackStateInterrupted :
break;
}
}
Related
Hi everyone I am working on an application in which I have a url of video and I have to play a video from that url. I have done this job from this code
- (IBAction)btnPlayVideo:(id)sender
{
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
[self.view addSubview:mPlayerVC.view];
//play movie
MPMoviePlayerController *player = [mPlayerVC moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
player.view.frame = self.view.frame;
[player setFullscreen:YES animated:YES];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
//============Other Methods====================
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
break;
default:
break;
}
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
}
My problem is that when this code run video player open and start loading and it takes too much time to run a video. Can anybody guide me how to run video in fast way from internet?
There's nothing in the code that suggests that lag is anything other than the time it takes to make the request and sufficiently buffer. The most common technique to improve UE is to start loading as early as possible, even before the user requests playback.
If this is possible, the code should be reorganized as follows:
// hang on to the movie player
#property(nonatomic,retain) MPMoviePlayerController *mp;
// call this as soon as its possible to know the user might want to see the video
- (void)primeVideo {
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
MPMoviePlayerController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
// do this also in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
mp.shouldAutoplay = NO;
[mp prepareToPlay];
self.mp = mp;
}
That does as much prep as possible without changing the UI. The rest of your code from the button action is left, tweaked a little...
- (IBAction)btnPlayVideo:(id)sender {
// if there's any way that the user can request playback before
// you've called primeVideo, check for that here. But hopefully you
// can call primeVideo before user even sees the play button
if (!self.mp) [self primeVideo];
self.mp.view.frame = self.view.frame;
[self.mp setFullscreen:YES animated:YES];
[self.view addSubview:self.mp.view];
MPMovieLoadState state = [self.mp loadState];
if (state & MPMovieLoadStatePlaythroughOK) [self.mp play];
else self.mp.shouldAutoplay = YES;
}
i click on the button here is the code
//NSURL *url = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:self.urlFile];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
_moviePlayer.view.frame = CGRectMake(0, 0, 320, 300);
[_moviePlayer setFullscreen:YES animated:YES];
and it plays but when i press the Done button, here is the method
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
player.initialPlaybackTime = -1;
[player.view removeFromSuperview];
}
it looks like this
sorry dont have enough to post image
it has the black lining on the bottom and the play button disappears as well, how do i load it back to the point it was when the cell was pressed and came to the detailViewController
I am playing video in MPmovieController it works fine but player removes when complete video is played.I want that when user pressed done button then video should stop here is the code i am using.
NSURL*myURL=[NSURL fileURLWithPath:url];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:myURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
Try this one:
NSURL *fileURL=[NSURL URLWithString:[[array objectAtIndex:videoid] valueForKey:#"VideoUrl"]];
self.mpPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:self.mpPlayer];
[self.mpPlayer.moviePlayer prepareToPlay];
self.mpPlayer.moviePlayer.shouldAutoplay=NO;
[self.mpPlayer.moviePlayer play];
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer pause];
[self dismissMoviePlayerViewControllerAnimated];
// [moviePlayer.view removeFromSuperview];
}
may be it will help.
happy coding...
remove this line into your then it will work
[moviePlayerController.view removeFromSuperview];
I'm trying to prevent the transition animation that occurs when you exit a video in MPMoviePLayerController when the user presses done. I can stop it fine when the movie finishes on its own using the moviePlayBackDidFinish: notification but for some reason when I try it in the exact same way with the exitedFullscreen: notification (which responds to a done press) it doesn't work, as in, the animation occurs.
Here is the complete code, any help would be much appreciated.
-(void) playvideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test" ofType:#"MOV"]];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
}
-(void)moviePlayBackDidFinish: (NSNotification*)notification
{
moviePlayer.fullscreen = NO;
[moviePlayer.view removeFromSuperview];
}
- (void)exitedFullscreen:(NSNotification*)notification {
moviePlayer.fullscreen = NO;
[moviePlayer.view removeFromSuperview];
}
you can to remove animation using this code .
-(void)moviePlayBackDidFinish: (NSNotification*)notification
{
[moviePlayer stop];
[moviePlayerController.view removeFromSuperview];
moviePlayer = nil;
}
- (void)exitedFullscreen:(NSNotification*)notification {
[moviePlayer stop];
[moviePlayerController.view removeFromSuperview];
moviePlayer = nil;
}
I'm running the following code. The video plays fine but after it finishes it just goes to a black srcreen, my original view never comes back. When I tap on the black screen i just see the message "loading....." Can someone please explain what I'm doing wrong. Thanks
- (IBAction)video:(UIBarButtonItem *)sender
{
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"IMG_0973" ofType:#"MOV"]];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
moviePlayer.controlStyle=MPMovieControlStyleDefault;
//moviePlayer.shouldAutoplay=NO;
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
moviePlayer=nil;
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
}
}
Add this notification Method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
object:player];
This method is called after your movie is loaded and in this method you add your moviePlayer view.
-(void)moviePreloadDidFinish:(NSNotification*)notification
{
moviePlayer.controlStyle=MPMovieControlStyleDefault;
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
[moviePlayer setFullscreen:YES animated:YES];
}
In your video IBAction, you need to add the subview before you tell it to play. Switch the lines [moviePlayer play] and [self.view addSubview:moviePlayer.view]. Let us know it it works! Actually you may need to place moviePlayer play even after the full screen line.
i think this will help you .....
-(void)playVideo
{
NSString *contentURL = [[NSBundle mainBundle] pathForResource:#"xyz" ofType:#"mp4"];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController)
{
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[moviePlayerViewController.moviePlayer setFullscreen:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MovieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController.moviePlayer play];
[navi presentModalViewController:moviePlayerViewController animated:NO];
[moviePlayerViewController release];
moviePlayerViewController = nil;
}
}
-(void)MovieFinished:(NSNotification *)notification
{
MPMoviePlayerController *player = (MPMoviePlayerController *)notification.object;
[player stop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
//do rest of the stuff
}