MPMoviePlayerViewController在后台退出

时间:2012-04-12 10:50:47

标签: objective-c ios mpmovieplayercontroller

当我的应用程序转到后台并返回时,模态视图关闭。为什么会这样?我试着暂停一下。如果它是有用的信息,我已启用ARC。

MPMoviePlayerViewController * player = [[MPMoviePlayerViewController alloc] initWithContentURL:targetURL];
[player.moviePlayer prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:player];

4 个答案:

答案 0 :(得分:5)

如果其他人偶然发现了这一点(就像我之前找到自己的解决方案一样),我可以阻止MPMoviePlayerViewController在应用程序背景时通过继承它并将其作为{{{}的观察者移除而退出1}}:

UIApplicationDidEnterBackgroundNotification

答案 1 :(得分:2)

您不需要继承MPMoviePlayerViewController - 只需在创建MPMoviePlayerViewController实例后执行以下操作。

[[NSNotificationCenter defaultCenter] removeObserver:self.moviePlayerViewController name:UIApplicationDidEnterBackgroundNotification object:nil];

答案 2 :(得分:0)

以下是可以帮助您的完整代码

// Call this to start initialization and play movie
-(void)prepareMoviePlayer:(NSURL *)moviePath
{
    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:moviePath];

    if ([[moviePlayerViewController moviePlayer] respondsToSelector:@selector(loadState)])
    {
        [[moviePlayerViewController moviePlayer] setControlStyle:MPMovieControlStyleNone];
        [[moviePlayerViewController moviePlayer] setFullscreen:YES];
        [[moviePlayerViewController moviePlayer] prepareToPlay];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(moviePlayerLoadStateDidChange:)
                                              name:MPMoviePlayerLoadStateDidChangeNotification
                                              object:nil];
    }
}

- (void) addPlayer
{
    [[[moviePlayerViewController moviePlayer] view] setFrame:self.view.bounds];
    [[self view] addSubview:[[moviePlayerViewController moviePlayer] view]];
}

static NSTimeInterval t;
// Call this on applicationWillResignActive
-(void) pauseMovieInBackGround
{
    [[moviePlayerViewController moviePlayer] pause];
    t = [[moviePlayerViewController moviePlayer] currentPlaybackTime];
    [moviePlayerViewController.view removeFromSuperview];
}

// Call this on applicationWillEnterForeground
-(void) resumeMovieInFrontGround
{
    [self addPlayer];

    [[moviePlayerViewController moviePlayer] stop];
    [[moviePlayerViewController moviePlayer] setInitialPlaybackTime:t];
    [[moviePlayerViewController moviePlayer] play];
}

-(void)moviePlayerLoadStateDidChange:(NSNotification *)notification
{
    if([[moviePlayerViewController moviePlayer] loadState] != MPMovieLoadStateUnknown)
    {

        [[NSNotificationCenter defaultCenter] removeObserver:self
                                              name:MPMoviePlayerLoadStateDidChangeNotification
                                              object:nil];

        [self addPlayer];
        [[moviePlayerViewController moviePlayer] play];
    }
}

-(void)videoPlayBackDidFinish:(NSNotification*)notification
{
    [self dismissMoviePlayerViewControllerAnimated];
}

答案 3 :(得分:0)

我遇到了这个问题。
我已经尝试了ChrisH的答案,但在我将removeObserver放入viewDidLoad()而不是init()之前,它对我不起作用。

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

}

然后它有效。享受它。