Note that there are some explanatory texts on larger screens.

plurals
  1. POMPMoviePlayerViewController backgrounding deal. How to continue playing after returning from background to foreground?
    primarykey
    data
    text
    <p>I'm working on application for video streaming. It is built with ARC for iOS5. To display view I use <em>MPMoviePlayerViewController</em> this way:</p> <p>.h </p> <pre><code>@interface EpisodesTableViewController : UITableViewController&lt;EpisodeUrlResolverDelegate&gt; { NSTimeInterval playbackTime; EpisodeUrlResolver *episodeUrlResolver; } @property (strong, nonatomic) MPMoviePlayerViewController *player; @end </code></pre> <p>.m</p> <pre><code>@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]; } </code></pre> <p>Few words about presented code: when url for video is resolved I create the <em>MPMoviePlayerViewController</em> object for video playback. Playback is started. Then when app is going background with home button <em>MPMoviePlayerViewController</em> 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 <em>MPMoviePlayerViewController</em> 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.</p> <p>But it doesn't work when user locks the device. <em>WillEnterBackground</em> notification is fired when <em>applicationWillEnterForeground</em> 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 </p> <pre><code>[self presentModalViewController:player animated:YES]; </code></pre> <p>throws the exception </p> <pre><code>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller &lt;EpisodesTableViewController: 0x91b3950&gt;.' </code></pre> <p>I tried to change the code to dismiss controller:</p> <pre><code>- (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]; } } } </code></pre> <p>Now it doesn't throw the exception, but displays warning </p> <pre><code>wait_fences: failed to receive reply: 10004003 </code></pre> <p>Also <em>completion</em> block is never called.</p> <p><strong>How to properly to solve the background/foreground continue playing with modally displayed MPMoviePlayerViewController?</strong> Thanks for replies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload