MPMoviePlayerViewController backgrounding交易。 如何继

2019-07-30 22:41发布

我正在申请视频流。 它是建立与ARC对iOS5的。 要显示视图我用MPMoviePlayerViewController这种方式:

。H

@interface EpisodesTableViewController : UITableViewController<EpisodeUrlResolverDelegate> {
    NSTimeInterval playbackTime;
    EpisodeUrlResolver *episodeUrlResolver;
}
@property (strong, nonatomic) MPMoviePlayerViewController *player;
@end

.M

@implementation EpisodesTableViewController
@synthesize episodes, player;

- (void)viewDidLoad
{
    [super viewDidLoad];
    episodeUrlResolver = [[Soap4MeEpisodeUrlResolver alloc] init];
    [episodeUrlResolver setDelegate:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:@"WillEnterForeground" object:nil];                 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterBackground) name:@"WillEnterBackground" object:nil];
}

- (void)viewDidUnload
{
    episodeUrlResolver = nil;
    player = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"WillEnterForeground" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"WillEnterBackground" object:nil];
    [super viewDidUnload];
}

- (void)url:(NSURL *)url WasResolvedForEpisode:(Episode *)episode {
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    player.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    player.moviePlayer.allowsAirPlay = YES;
    [self presentModalViewController:player animated:YES];
}

- (void)willEnterBackground {
    if (player) {
        playbackTime = player.moviePlayer.currentPlaybackTime;
    }
}

- (void)willEnterForeground {
    if (!player)
        return;
    if(player.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || player.moviePlayer.playbackState == MPMoviePlaybackStateStopped || player.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
    {
        [self continuePlayback];
    }
}

- (void)continuePlayback {
    [self presentModalViewController:player animated:YES];
    [player.moviePlayer setInitialPlaybackTime:playbackTime];
    NSLog(@"%f", player.moviePlayer.initialPlaybackTime);
    [player.moviePlayer play];
}

关于提交代码几句话:当网址的视频解决我创建视频播放MPMoviePlayerViewController对象。 播放开始。 然后,当应用即将背景的主页按钮MPMoviePlayerViewController自动暂停从屏幕播放和自败(怪异的行为,因为对我来说)。 这就是为什么我有当应用即将背景,以节省播放时间,并显示出与恢复播放时间再次现有MPMoviePlayerViewController当应用程序是回到前台。 当用户按下home键,然后回到应用程序,它工作正常。

但是,当用户锁定设备不工作。 所以对于锁定事件也被称为太当applicationWillEnterForeground被称为WillEnterBackground通知解雇。 但在情况下,当用户锁定MPMoviePlayerViewController是当用户按下home键没有被正确关闭在形势的装置。 它是从屏幕隐藏,但调用的代码

[self presentModalViewController:player animated:YES];

抛出异常

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <EpisodesTableViewController: 0x91b3950>.'

我试图修改代码以关闭控制器:

- (void)willEnterForeground {
        if (!player)
            return;
        if(player.moviePlayer.playbackState == MPMoviePlaybackStateInterrupted || player.moviePlayer.playbackState == MPMoviePlaybackStateStopped || player.moviePlayer.playbackState == MPMoviePlaybackStatePaused)
        {
            if (self.presentedViewController) {
            [self dismissViewControllerAnimated:YES completion:^{
                [self continuePlayback];

            }];
        }
        else {
            [self continuePlayback];
        }
        }
    }

现在,它不会抛出异常,但显示警告

wait_fences: failed to receive reply: 10004003

完成块永远不会被调用。

如何妥善解决的背景/前景继续与模态显示MPMoviePlayerViewController玩? 感谢您的答复。

Answer 1:

我解决了这个问题。

方法-willEnterBackground应该是这样的:

- (void)willEnterBackground {
    if (player) {
        playbackTime = player.moviePlayer.currentPlaybackTime;
        [self dismissModalViewControllerAnimated:NO];
    }
}

我们必须手动解雇MPMoviePlayerViewController当应用程序正进入后台模式。 注意只使用

[self dismissModalViewControllerAnimated:NO];

代替

[self dismissMoviePlayerViewControllerAnimated];

那是因为MPMoviePlayerViewController表现出与

[self presentModalViewController:player animated:YES];


文章来源: MPMoviePlayerViewController backgrounding deal. How to continue playing after returning from background to foreground?