MPMoviePlayerViewController

MPMoviePlayerViewController

本文介绍了MPMoviePlayerViewController后台交易。从背景返回前景后如何继续播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正致力于视频流的应用。它是使用ARC for iOS5构建的。
要显示视图,我用这种方式使用 MPMoviePlayerViewController

I'm working on application for video streaming. It is built with ARC for iOS5.To display view I use MPMoviePlayerViewController this way:

.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 ,并在应用程序返回前台时恢复播放时间。
当用户按下主页按钮然后返回应用程序时它工作正常。

Few words about presented code:when url for video is resolved I create the MPMoviePlayerViewController object for video playback. Playback is started. Then when app is going background with home button MPMoviePlayerViewController automatically pauses playback and dissapears from screen (weird behavior, as for me). Thats why I have to save playback time when app is going background and show existing MPMoviePlayerViewController again with restored playback time when app is back to foreground.It works fine when user presses home button and then is going back to application.

但是当用户锁定设备时它不起作用。当调用
applicationWillEnterForeground 时会触发 WillEnterBackground 通知,因此对于锁定事件,也会调用它。
但是在用户锁定设备的情况下,MPMoviePlayerViewController没有被正确解雇,因为用户按下主页按钮的情况。它在屏幕上隐藏,但调用代码

But it doesn't work when user locks the device. WillEnterBackground notification is fired whenapplicationWillEnterForeground is called, so for locking event it is called too.But in case when user locks the device MPMoviePlayerViewController wasn't being dismissed properly as in situation when user presses home button. It is hidden from the screen, but calling the code

[self presentModalViewController:player animated:YES];

抛出异常

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

我试图更改代码以解除控制器:

I tried to change the code to dismiss 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];
        }
        }
    }

现在它不会抛出例外,但显示警告

Now it doesn't throw the exception, but displays warning

wait_fences: failed to receive reply: 10004003

同样完成块永远不会被调用。

Also completion block is never called.

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

How to properly to solve the background/foreground continue playing with modally displayed MPMoviePlayerViewController?Thanks for replies.

推荐答案

I solved the problem.

方法 -willEnterBackground 应如下所示:

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

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

We have to dismiss MPMoviePlayerViewController manually when app is entering background mode.Pay attention to use only

[self dismissModalViewControllerAnimated:NO];

而不是

[self dismissMoviePlayerViewControllerAnimated];

那是因为 MPMoviePlayerViewController 显示为

[self presentModalViewController:player animated:YES];

这篇关于MPMoviePlayerViewController后台交易。从背景返回前景后如何继续播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:53