1

当我的应用程序进入后台并返回时,模式视图会关闭。为什么会这样?我尝试打开和关闭暂停。我启用了 ARC,如果它是有用的信息。

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

4 回答 4

5

万一其他人偶然发现了这一点(就像我在找到自己的解决方案之前所做的那样),我可以MPMoviePlayerViewController通过将其子类化并将其作为以下观察者删除来防止在应用程序背景时退出UIApplicationDidEnterBackgroundNotification

- (id)initWithContentURL:(NSURL *)contentURL {

    self = [super initWithContentURL:contentURL];

    if (self){

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

    }

    return self;
}
于 2013-05-16T21:55:28.817 回答
2

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

[[NSNotificationCenter defaultCenter] removeObserver:self.moviePlayerViewController name:UIApplicationDidEnterBackgroundNotification object:nil];
于 2014-08-18T22:32:33.830 回答
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];
}
于 2012-09-21T11:43:00.297 回答
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];

}

然后它工作。好好享受。

于 2013-07-10T02:08:19.517 回答