MPMoviePlayerController - black screen when switching views - iphone

The background image is an animated sky
When switching between settings page (static image) and the home page (animated image) the screen animation often drops off and becomes a single black image
Can anybody suggest a reason or a solution?
Thanks!
Henry Colour Vision Apps
- (void)viewDidLoad
{
[super viewDidLoad];
self.mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"iPhone4" ofType:#"mov"]]];
mp.repeatMode = MPMovieRepeatModeOne;
[mp.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mp.scalingMode = MPMovieScalingModeFill;
[mp setFullscreen:YES];
mp.controlStyle = MPMovieControlStyleNone;
[self.view addSubview:mp.view];
[self.view sendSubviewToBack:mp.view];
[mp prepareToPlay];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mp play];
}

Try add a observer to your player. Like this:
- (void)viewDidLoad
{
///...
self.mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"iPhone4" ofType:#"mov"]]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.mp];
///...
}
Then you can get what's wrong:
- (void)moviePlaybackComplete:(NSNotification *)notification
{
NSLog(#"Movie Finished.");
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackError) {
NSDictionary *notificationUserInfo = [notification userInfo];
NSError *mediaPlayerError = [notificationUserInfo objectForKey:#"error"];
if (mediaPlayerError)
{
NSLog(#"playback failed with error description: %#", [mediaPlayerError localizedDescription]);
}
else
{
NSLog(#"playback failed without any given reason");
}
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.mp];
}

Related

MPMoviePlayerController doesnot play the video its full length

I am using the MPMoviePlayerController to open a video file.The video runs fine.But suppose if the video file is 10 sec ,with 1 sec remaining the video stops.Is that its natural way of playing or should we specify something.The follow is the code used
NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];
- (void)moviePlaybackComplete:(NSNotification *)notification {
NSNumber *finishReason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
self.moviePlayerController = [notification object];
self.moviePlayerController.view.hidden = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController release];
}
[self dismissViewControllerAnimated:YES completion:nil];
}

MPMoviePlayer controlStyle

I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_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 = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(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;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}

How to show custom view when user click on thumb in TTThumbsViewController?

I have created TTThumbsViewController with thumbs (from json data) and when user clicks on thumb, my app must open video in new subview like this:
- (void)thumbsViewController: (TTThumbsViewController*)controller
didSelectPhoto: (id<TTPhoto>)photo
{
NSMutableArray *photoset = [[NSMutableArray alloc] initWithArray:[self.photoSource photos]];
Photo *selected = [photoset objectAtIndex:[photo index]];
NSLog(#"%#", [selected urlLarge]);
NSURL *url = [NSURL URLWithString:[selected urlLarge]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
} else {
[moviePlayer play];
}
}
NSLog shows that thumb clicked but it open default TTPhotoViewController :( I want to disable and show only this subview.
This helps me and solve my problem
- (void)thumbsTableViewCell:(TTThumbsTableViewCell*)cell didSelectPhoto:(id<TTPhoto>)photo {
[_delegate thumbsViewController:self didSelectPhoto:photo];
}

iPhone MediaPlayer - Fullscreen?

I've gone through a bunch of other examples, but I cannot get a video to load full screen in a viewController.
I'm able to get it to load... but not launch in full screen.
The code is below. Thanks!
-(void)viewWillAppear:(BOOL)animated {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Sample" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player autorelease];
}
Edit: Full screen fixed... I just had to add: moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
Now it adds an odd image to my status bar.. see attached photo.
I don't know what you mean by fullscreen means, but I normally see this line
[moviePlayerController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
written with bounds as an argument like this
[moviePlayerController.view setFrame:self.bounds];
or
[moviePlayerController.view setFrame:self.view.bounds];
depending on what type of object self is.

MPMoviePlayerController not released

I am running an intro movie in a MPMoviePlayerController and i've got a problem with the allocated memory not being released.
When the movie finishes, Instruments shows the memory being released.
However when i skip the movie by tapping on it the memory is not released.
Here are the important parts of my code:
- (void)applicationDidFinishLaunching:(UIApplication *)theApplication {
self.application = theApplication;
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"mymovie.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[window addSubview:moviePlayer.view];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.view.frame = window.frame;
UIView *introFrontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
introFrontView.userInteractionEnabled = YES;
introFrontView.opaque = NO;
introTabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(introTabFrom:)];
[introFrontView addGestureRecognizer:introTabRecognizer];
[moviePlayer.view addSubview:introFrontView];
[introFrontView release];
[introTabRecognizer release];
self.moviePlayerController = moviePlayer;
[moviePlayer release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(introFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController play];
}
- (void)introTabFrom:(UITapGestureRecognizer*)recognizer{
[[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
}
- (void) introFinished:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController pause];
[self.moviePlayerController stop];
[self.moviePlayerController release];
...
}
Is there anything i forgot? To my understanding i'm posting the correct notification...
Any ideas?
You may want to register for MPMoviePlayerPlaybackStateDidChangeNotification, and look at the playbackState that is passed in.