gpt4 book ai didi

objective-c - MPMoviePlayerViewController 后台处理。从后台返回前台后如何继续播放?

转载 作者:行者123 更新时间:2023-11-29 11:10:11 25 4
gpt4 key购买 nike

我正在处理视频流应用程序。它是用 ARC for iOS5 构建的。为了显示 View ,我以这种方式使用 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];
}

关于提供的代码的几句话:当视频的 url 被解析后,我创建了用于视频播放的 MPMoviePlayerViewController 对象。播放开始。然后当应用程序使用主页按钮进入后台时 MPMoviePlayerViewController 自动暂停播放并从屏幕上消失(奇怪的行为,对我来说)。这就是为什么我必须在应用程序进入后台时节省播放时间,并在应用程序返回前台时再次显示现有的 MPMoviePlayerViewController 并恢复播放时间。当用户按下主页按钮然后返回应用程序时,它工作正常。

但当用户锁定设备时它不起作用。 WillEnterBackground 通知在applicationWillEnterForeground 被调用,因此对于锁定事件它也被调用。但是如果用户锁定设备时 MPMoviePlayerViewController 没有像用户按下主页按钮时那样被正确关闭。它在屏幕上是隐藏的,但是调用代码

[self presentModalViewController:player animated:YES];

抛出异常

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

我试图更改代码以关闭 Controller :

- (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

completion block 也永远不会被调用。

如何正确解决背景/前景继续播放模态显示的 MPMoviePlayerViewController?感谢您的回复。

最佳答案

我解决了这个问题。

方法 -willEnterBackground 应该如下所示:

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

当应用进入后台模式时,我们必须手动关闭 MPMoviePlayerViewController。注意只用

[self dismissModalViewControllerAnimated:NO];

代替

[self dismissMoviePlayerViewControllerAnimated];

那是因为 MPMoviePlayerViewController 显示为

[self presentModalViewController:player animated:YES];

关于objective-c - MPMoviePlayerViewController 后台处理。从后台返回前台后如何继续播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com