MPMoviePlayerViewController becomes black when enters background - iphone

I have problem with MPMoviePlayerViewController , when app enters background and then I launch it again or go another viewControllers the movie became black ! I have movie which plays in the background of my menus , here is my code :
EIDTED CODE :
-(void)viewDidLoad {
[self moviePlayer2];
}
- (void) moviePlayer2 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"cloud" ofType:#"mp4"];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
player.view.userInteractionEnabled = YES;
player.moviePlayer.repeatMode = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeFill;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
[[player moviePlayer] play];
[self.view addSubview:player.view];
}
-(void) moviePlayBackStateChange: (NSNotification *) note {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:[player moviePlayer]];
[[player moviePlayer] play];
//[player release];
NSLog(#"FINISHED");
}
thank you .

I think you may need to add codes below:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:[player moviePlayer]];
and handle the movie state in the moviePlayBackStateChange method.
The movie will be paused when the movie is playing and the app enters in background, so you need to make the movie resume like below when the app come back from background. If not,the movie will keep the pause state. That's why your app becomes black.
[[player moviePlayer] play];
then the movie will continue to play.
adding two methods which you should invote when the app comes into background and backs from background:
-(void) pauseMovieInBackGround
{
[player moviePlayer] pause];
[player.view removeFromSuperview];
}
-(void) resumeMovieInFrontGround
{
[self.view addSubview:player.view];
[[player moviePlayer] play];
}
Hope this can help you guy.

Try changing this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[player release];
To this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:[player moviePlayer]];
[movieController.view removeFromSuperview];
[player release];
See if that works :D

Related

play video in ios6

When the aplicattion starts, the video will run automatically
but pressing the button home and open again the app freezes and do not know why
what can i do?
- (void)viewDidLoad{
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
m_player = nil;
}
}
This code help me to avoid freezes app when press button home and the video is play and works!!!
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleEnteredBackground:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
-(void)handleEnteredBackground:(NSNotification*)_notification{
[m_player play];
}

MPMoviePlayerViewController disappears when home button is pressed

I’m creating my MPMoviePlayerViewController like so:
MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:playerView];
It works fine until I press the home button: that way, after I reopen the app, the player view controller is gone and I’m seeing the view controller I originally called it from. The player doesn’t disappear but simply pauses (as it should) if I double-press the home button, opening the multitasking panel.
So how do I prevent this from happening (closing and reopening the app should bring me back to playing the video)?
UPD:
I noticed that the player doesn’t disappear if I close and reopen the app while the “Loading…” text is up. In that case, coming back to the app lets the player continue loading the video to eventually start playing. However closing the app after that (either when the video is playing or being paused) inevitably kills the player view controller…
Try like this:
MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
playerView.view.frame = self.view.frame;
[self presentMoviePlayerViewControllerAnimated:playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playbackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:moviePlayerController];
[playerView.moviePlayer play];
after that adding these two methods:
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
moviePlayerController = [aNotification object];
[moviePlayerController.moviePlayer stop];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController autorelease];
NSLog(#"stopped?");
}
- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[moviePlayerController.moviePlayer stop];
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[moviePlayerController dismissMoviePlayerViewControllerAnimated];
}
I found the answer here:
MPMoviePlayerViewController gets dismissed, when app resignes active state
The author of that question (and the answer) mentions however that his app was rejected for this.
you try this code---
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[self.movieController.view removeFromSuperview];
self.movieController = 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");
break;
default:
break;
}
[self.movieController setFullscreen:NO animated:YES];
}
- (void)showMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSURL* movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tron" ofType:#"mov"]];
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.movieController.view.frame = self.view.frame;
[self.view addSubview:movieController.view];
[self.movieController setFullscreen:YES animated:YES];
[self.movieController play];
}
MPMoviePlayerViewControllers dismiss when the app goes into the background, as Arnold stated, and can also dismiss when you leave the view controller view it's embedded in. To resolve both issues, try retaining the MPMoviePlayerViewController as an ivar or property and have it reset itself when the vc's viewWillAppear: is called or when the application re-enters the foreground, like:
- (void) viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reset) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void) viewWillAppear:(BOOL)animated{
[self reset];
}
- (void) reset{
[_mpmpvc.moviePlayer prepareToPlay];
[_mpmpvc.moviePlayer pause];
}

How to get video player to NOT auto play while using MPMoviePlayerController

I'm trying to code a video into my iPhone app and the code below works perfectly but the only thing is that I rather the video not automatically start playing when the user opens the page. I want them to be able to push the play button. The code below is located in my viewcontroller.m file.
(void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle] pathForResource:#"77860_00_01_MM02_welcome"
ofType:#"mov"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(10, 235, 150, 125);
[self.view addSubview:player.view];
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[player play];
}
Never mind, I figured it out. I just added [player prepareToPlay] under player.shouldAutoplay = NO
Don't put it in your viewDidLoad. Just create an IBAction them link it up to your trigger button.

integrate video in iphone app

I have to integrate a video into my iphone app.The difference from all the code that there is on the net is that I have to show the video and give to the user the chance to play it.Not to play it automatically.
And here is what I have done:
- (void) play
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"video1" ofType:#"mp4"];
NSURL *fileUrl1 = [NSURL fileURLWithPath:path];
videoPlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl1];
[videoPlayer1 setControlStyle:MPMovieControlStyleNone];
[[videoPlayer1 view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//[videoPlayer1 play];
[self.view addSubview:[videoPlayer1 view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[videoPlayer1.view removeFromSuperview];
[videoPlayer1 stop];
[videoPlayer1 release];
videoPlayer1 = nil;
}
But the screen just turn black and nothing else shown.
Unless I do [videoPlayer1 play] nothing happens.But I don't want to play the video automatically...it must be played by the user.
So, how should I act in order to show up the video(!not the black screen) and play it when the user wishes?Thanks
You are making an error and here it is
Change this
[videoPlayer1 setControlStyle:MPMovieControlStyleNone];
to
[videoPlayer1 setControlStyle:MPMovieControlStyleDefault];
You are hiding the controls by using that line of yours.
Cheers!!!

MPMoviePlayerController not working in iOS4

I'm using the first answer from
iOS 4 + MPMoviePlayerController
to try and get MPMoviePlayerController to play. In the simulator (iPhone device with iOS4), I hear sound but no video. On the device (iPhone 3GS and iOS4), I don't get anything. Here's my code:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.movieControlMode = MPMovieControlModeDefault;
if ([moviePlayer respondsToSelector:#selector(view)]) {
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayer.view];
}
[moviePlayer play];
Any ideas what I'm doing wrong?
I just tested the following code on iOS4 + iPhone 4 (and a 3GS)—it works fine. At first blush, I think your code's problem is not calling setFullscreen:animated on your MPMoviePlayerController instance.
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[self.movieController.view removeFromSuperview];
self.movieController = 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");
break;
default:
break;
}
[self.movieController setFullscreen:NO animated:YES];
}
- (void)showMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSURL* movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tron" ofType:#"mov"]];
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if ([self.movieController respondsToSelector:#selector(view)]) {
self.movieController.view.frame = self.view.frame;
[self.view addSubview:movieController.view];
[self.movieController setFullscreen:YES animated:YES];
}
[self.movieController play];
}
// This method is set as the action for an on-screen button
- (void)movieTime:(id)sender {
[self showMovie];
}
For MPMoviePlayerController view is a property, not a method, so you can't use respondsToSelector: on it if it doesn't have methods synthesized or written for it (i.e. is declared #dynamic). Which I think is true for most readonly properties in UIKit.