0
votes

I have problem with MPMoviePlayerViewController , when app enters background and then I launch it again the movie became black ! I have movie which plays in the background of my menus , here is my code :

This is my AppDelegate.m

  • (void)applicationWillResignActive:(UIApplication *)application

{

[[NSNotificationCenter defaultCenter] postNotificationName:@"WillResignActive" object:nil];

}

  • (void)applicationDidEnterBackground:(UIApplication *)application

{

[[NSNotificationCenter defaultCenter] postNotificationName:@"WillResignActive" object:nil];

}

  • (void)applicationWillEnterForeground:(UIApplication *)application

{

[[NSNotificationCenter defaultCenter] postNotificationName:@"WillEnterForeGround" object:nil];

}

  • (void)applicationDidBecomeActive:(UIApplication *)application

{

[[NSNotificationCenter defaultCenter] postNotificationName:@"DidBecomeActive" object:nil];

}

ViewController.m

  • (void)viewDidLoad

{

[super viewDidLoad];
// Do any additional setup after loading the view.

NSURL *url = [NSURL URLWithString:@"http://1080Digital.stream/playlist.m3u8"];

mp = [[MPMoviePlayerViewController alloc]initWithContentURL:url];

[self presentMoviePlayerViewControllerAnimated:mp];

mp.view.frame = self.view.bounds; //Set the size

self.view.backgroundColor=[UIColor clearColor];

mp.moviePlayer.controlStyle =MPMovieControlStyleNone;

mp.moviePlayer.scalingMode = MPMovieScalingModeFill;

mp.moviePlayer.repeatMode = MPMovieRepeatModeOne;

mp.moviePlayer.view.userInteractionEnabled = YES;

mp.moviePlayer.movieSourceType=MPMovieSourceTypeStreaming;

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AppDidBecomeActive) name:@"DidBecomeActive" object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnteredBackground) name:@"WillResignActive" object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnteredForeground) name:@"WillEnterForeGround" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackStateChange)
                                             name:MPMoviePlayerPlaybackStateDidChangeNotification
                                           object:[mp moviePlayer]];

[mp.moviePlayer prepareToPlay];

[mp.moviePlayer prepareToPlay];

[mp.moviePlayer play];


[self.view addSubview:mp.view];

}

-(void)moviePlayBackStateChange

{

[[mp moviePlayer] play];

}

-(void)AppDidBecomeActive

{

if(mp.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || mp.moviePlayer.playbackState == MPMoviePlaybackStateStopped || mp.moviePlayer.playbackState == MPMoviePlaybackStatePaused)

{

    [mp.moviePlayer play];

}

}

-(void)EnteredBackground

{

[[mp moviePlayer] pause];

[mp.view removeFromSuperview];

}

-(void)EnteredForeground {

[self.view addSubview:mp.view];

[[mp moviePlayer] play];

}

1
Why are you removing the view from superview when entering background?Adeel Ur Rehman
In EnteredBackground, do not remove it, only pause it. And in enteredforeground, only play no need add it again.Mrunal

1 Answers

2
votes

only u need to implemement

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnteredForeground) name:@"WillEnterForeGround" object:nil];

and in

(void)applicationWillEnterForeground:(UIApplication *)application

{

[[NSNotificationCenter defaultCenter] postNotificationName:@"WillEnterForeGround" object:nil];

}