Surya
Surya

Reputation: 39

How to play MPMoviePlayerviewController when enters from background to foreground in ios8?

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

{

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

}

{

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

}

{

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

}

{

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

}

ViewController.m

{

[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];

}

Upvotes: 0

Views: 479

Answers (1)

Arvind Kumar
Arvind Kumar

Reputation: 2411

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];

} 

Upvotes: 2

Related Questions