如何在ios8中从后台进入前台时播放MPMoviePlayerviewController?

时间:2014-12-18 08:55:36

标签: ios objective-c iphone xcode

我有MPMoviePlayerViewController的问题,当应用程序进入后台然后我再次启动它时,电影变黑了!我的电影在我的菜单背景中播放,这是我的代码:

这是我的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

  • (无效)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];

}

- (无效)moviePlayBackStateChange

{

[[mp moviePlayer] play];

}

- (无效)AppDidBecomeActive

{

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

{

    [mp.moviePlayer play];

}

}

- (无效)EnteredBackground

{

[[mp moviePlayer] pause];

[mp.view removeFromSuperview];

}

- (无效)EnteredForeground {

[self.view addSubview:mp.view];

[[mp moviePlayer] play];

}

1 个答案:

答案 0 :(得分:2)

只有你需要实施

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

and in

(void)applicationWillEnterForeground:(UIApplication *)application

{

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

}