I have an app with a tab bar controller at the root. The "home view" is a 3D rendered screen in OpenGL. There are certain 3D objects that you can click on that need to display a video. The video should be fullscreen and fade in and out.
To do this, I made the HomeViewController create a MPMoviePlayerViewController, assigned it a URL and then presented it from the tab bar controller. (I would have presented it from the HomeViewController but for some reason it didn't change its orientation properly--I'm sure it's related to all of the custom 3D stuff, and I didn't program it, so I just did a workaround by displaying it from a senior view.)
(Note that I am presenting the MPMoviePlayerViewController modally (not using the built-in presentModalMovieViewController or whatever) because Apple forces the transition to be the wacky screen-shift, and I wanted the dissolve.)
Now, that works fine. The modal window dissolves in, the video plays. You can play and pause it, fast forward, hit "Done" and the modal window goes away. Voila.
Now, here comes the totally weird bug: if you don't tap the video player and let the controls fade out (as they do after a second or two) the user cannot bring them back by tapping. It seems like the video controller stops responding to user input after that fade. Again, it works fine before they fade away. But after that point, I have to wait for the video to play all the way (at which point the modal window does, in fact, go away).
For reference, here is the relevant code to the modal video player:
-(void) startVideoWithURL:(NSURL *)videoURL {
if (!self.outsideMoviePlayerViewController) {
self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
}
if (videoURL) {
[self stopAnimation];
self.outsideMoviePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.outsideMoviePlayerViewController.moviePlayer.contentURL = videoURL;
[[[AppController instance] getCustomTabBarViewController] presentModalViewController:self.outsideMoviePlayerViewController animated:YES];
// Move observation of the dismiss from the MoviePlayerViewController to self.
[[NSNotificationCenter defaultCenter] removeObserver:self.outsideMoviePlayerViewController
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.outsideMoviePlayerViewController.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.outsideMoviePlayerViewController.moviePlayer];
}
}
-(void) movieFinishedCallback:(NSNotification *)aNotification {
// Summary: Restart 3D animation, unregister from notifications, kill the modal video.
[self startAnimation];
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[[AppController instance] getCustomTabBarViewController] dismissModalViewControllerAnimated:YES];
}
The only other reference I could find to an issue like this was some archived post on the Apple Support Communities, here:
https://discussions.apple.com/thread/2141156?start=0&tstart=0
In this thread, the issue poster figures it out himself, and states that the issue was resolved. Here's his explanation:
The problem occurs when CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE). After i changed to CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002, TRUE), the playback control can appears/disappear by tapping the screen.
Unfortunately, I'm not programming a game and nobody on my dev team calls CFRunLoopRunInMode anywhere in the code. The closest thing I found to this was in the animation code (in the same ViewController):
- (void)startAnimation
{
if (!animating)
{
NSLog(#"startAnimation called");
CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:#selector(drawFrame)];
[aDisplayLink setFrameInterval:animationFrameInterval];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.displayLink = aDisplayLink;
animating = TRUE;
}
}
If anyone has any insights on what could be causing this, I would appreciate it. I figured that, at the very least, even if I figure it out myself tonight, this problem could go up on Stack Overflow and be archived for the sake of posterity.
Cheers.
I figured out what was causing this problem.
I noticed that the first video did play, while successive ones did not. I moved this code:
if (!self.outsideMoviePlayerViewController) {
self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
}
This way the creation of the outsideMoviePlayerViewController was inside the next block, like so:
if (videoURL) {
self.outsideMoviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:nil];
[self stopAnimation];
self.outsideMoviePlayerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.outsideMoviePlayerViewController.moviePlayer.contentURL = videoURL;
Now a new controller is created instead of recycling the controller each time I played a video. The bug went away. I'm not 100% sure why this happened, because there are a variety of things that occur when you display a modal view controller.
The bottom line is probably that I should have done this in the first place as part of the lazy loading paradigm instead of trying to keep the controller in memory.
Related
I am facing an issue with MPMoviePlayerController in iOS 7. When i single tap on the forward seek button the video stops and not allow to do anything like to play again full screen and slider change.
Here is my code.
remove the Observer for the MPMoviePlayerPlaybackDidFinishNotification
[[NSNotificationCenter defaultCenter] removeObserver:moviePlayerViewController name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
and add New Notification MPMoviePlayerPlaybackDidFinishNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Here is my custom method to handle the MPMoviePlayerPlaybackDidFinishNotification
-(void)videoFinished:(NSNotification*)aNotification{
MPMoviePlayerController *moviePlayer = [aNotification object];
NSLog(#"%f",moviePlayer.currentPlaybackTime);
int reason = [[[aNotification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
}else if (reason == MPMovieFinishReasonUserExited) {
[self performSelector:#selector(dismiss:) withObject:aNotification afterDelay:0.5];
}else if (reason == MPMovieFinishReasonPlaybackError) {
}
}
I need to stop this strange behaviour on single click and continue to play.
Anyone know how to do this?
Thanks.
I think there are no any notifications or event are available on user
interaction with the standard player buttons, and i have to implement
own UI for the player controls. by this way we can then determine the
actions for a single touch, long touch, etc. Then, we can add whatever
functionality like increasing the play rate, or simply seeking to a
time.
An MPMoviePlayerViewController which is presented modally through presentMoviePlayerViewControllerAnimated: automatically dismisses itself when it's content finishes playing.
I've tried to disable this, since I want to play other content afterwards. However, even if I register to the NSNotificationCenter with [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; and set some other content, it still dismisses.
How can I stop MPMoviePlayerViewController from automatically dismissing itself?
UPDATE:
As a clarification, this question is only about removing the automatic dismissal and not about dealing with the disabled 'done' button. The selected answer reflects. This is by design, since we assume the developer adds their own means of dismissing the MPMoviePlayerViewController. However, #bickster's answer deals with the 'done' button as well.
Thanks to this blog article I figured out that MPMoviePlayerViewController automatically registers itself to the NSNotificationCenter upon creation. You have to first remove this registration and it will stop dismissing itself automatically.
// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer];
You can use this code to stop the viewcontroller from automatically dismissing and capture the event when the user clicks the "Done" button so you can dismiss the viewcontroller yourself.
Step 1. - alloc a MPMoviePlayerViewController
videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc ]initWithString:[aURL];
Step 2. - Remove the default MPMoviePlayerPlaybackDidFinishNotification observer and add your own
[[NSNotificationCenter defaultCenter] removeObserver:videoPlayer
name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer.moviePlayer];
Step 3. - Present viewcontroler
[self presentMoviePlayerViewControllerAnimated:videoPlayer];
Step 4. - Add videoFinish: method
-(void)videoFinished:(NSNotification*)aNotification{
int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (value == MPMovieFinishReasonUserExited) {
[self dismissMoviePlayerViewControllerAnimated];
}
}
You can try something like this.
when the mpmovieplayercontroller finishes playing a video and you recieve the notification in your method movieFinishedCallback: implemect
[playerVC.movieplayer setContentURL:// set the url of the file you want to play here];
[playerVC.moviePlayer play];
Hope this helps
Since "Done" button is not working anymore if I remove MPMoviePlayerPlaybackDidFinishNotification from NSNotificationCenter, I change repeat mode to MPMovieRepeatModeOne.
Then, everything's working fine except the video is repeated.
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
[playerVC.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
I'm trying to enable AirPlay support in my app. I'm not doing video; I want to use the external display as a "second display".
Here's my problem: if I choose "AppleTV" from my AirPlay button, my app doesn't get notified. The only time my app -does- get notified is when I leave my app, go to the OS-level AirPlay button, choose "AppleTV" there and turn on Mirroring. If I turn mirroring off, my app is then told that the external display is gone.
So:
Why doesn't my app get notified when I pick an external display from
within my app?
Why does my app get notified of the presence of an
external display when I turn mirroring on...and not before? I'm obviously misunderstanding something, but it would seem like turning mirroring on should inform my app that the external display is gone (rather than now available, since the OS should now be using that external display for mirroring.)
Code sample below. Thanks in advance for any help!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// Is there already an external screen?
if (UIScreen.screens.count > 1)]
{
[self prepareExternalScreen:UIScreen.screens.lastObject];
}
// Tell us when an external screen is added or removed.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(externalScreenDidConnect:) name:UIScreenDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(externalScreenDidDisconnect:) name:UIScreenDidDisconnectNotification object:nil];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
// Add AirPlay control to view controller.
MPVolumeView* airplayButtonView = [[MPVolumeView alloc] init];
airplayButtonView.frame = CGRectMake(300, 300, 50, 50);
airplayButtonView.backgroundColor = [UIColor blackColor];
airplayButtonView.showsVolumeSlider = NO;
airplayButtonView.showsRouteButton = YES;
[self.viewController.view addSubview:airplayButtonView];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - External screen handling
- (void)externalScreenDidConnect:(NSNotification*)notification
{
[self prepareExternalScreen:notification.object];
}
- (void)externalScreenDidDisconnect:(NSNotification*)notification
{
// Don't need these anymore.
self.externalWindow = nil;
}
- (void)prepareExternalScreen:(UIScreen*)externalScreen
{
NSLog(#"PREPPING EXTERNAL SCREEN.");
self.viewController.view.backgroundColor = [UIColor blueColor];
CGRect frame = externalScreen.bounds;
self.externalWindow = [[UIWindow alloc] initWithFrame:frame];
self.externalWindow.screen = externalScreen;
self.externalWindow.hidden = NO;
self.externalWindow.backgroundColor = [UIColor redColor];
}
That's correct, unfortunately. The secondary display (the airplay screen) is only available with mirroring.
Here is an application that shows how to implement this:
https://github.com/quellish/AirplayDemo
Looking at your code, you SHOULD be getting the UIScreenDidConnectNotification when a user goes to the airplay menu and turns on mirroring while your app is active. The "Airplay Button", wether a MPVolumeView or movie controller, does not control mirroring (and thus the external display functionality). Video and audio out are unfortunately separate from mirroring, and mirroring can only be turned on or off using the system wide mirroring UI.
Bottom line: You can't turn on that AirPlay screen from within your app.
Finally found the answer, you must have mirroring enabled in order to get the new screen notification, but then you should overwrite the screen with your second screen content. Very confusing!
See this example:
UIScreen screens always return 1 screen
Now, the worst part. You can add an AirPlay button inside your app using this:
MPVolumeView *volumeView = [ [MPVolumeView alloc] init] ;
[view addSubview:volumeView];
However you cannot enable mirroring from this picker! And there is no programmatic way to turn on mirroring.
How can I turn on AirPlay Screen Mirroring on the iPhone 4S programmatically
So apparently the only way to have a second screen experience is to instruct your user how to turn on AirPlay from multitasking bar and make sure they turn mirror on.
It seems not possible from inside of app unfortunately. Only airplay sound can be turned on from inside app afaik. Here's an example app using the second screen with OpenGL and sounds http://developer.apple.com/library/ios/samplecode/GLAirplay/Introduction/Intro.html
I've created an iPad app which contains a slideshow and when this slideshow is tapped by the user he/she can entered some information.
What I'd like to do now is to display the slideshow contents on a TV when connecting the TV and iPad through AirPlay (or cable if possible, but that only seems to mirror things)
Can this be done? Can we have the slideshow run on the TV and also on iPad and then when the user taps the slideshow on the iPad the credentials input screen will show but on TV still the underlying slideshow will show and not the credentials?
How can this be done in iOS? Is it possible to display a portion of the application on the TV? So not mirroring the entire application.
You can write the app to handle 2 UIScreens using Airplay and an Apple TV then set a seperate root view controller for both the TV UIScreen and for the iPad UIScreen. Then display the image or slideshow on the TV's view controller and run that from the events of you iPads view controller!
AMENDED AFTER CLIFS COMMENT:
So firstly in your app delegate in didFinishLaunchingWithOptions or didFinishLaunching setup a notification to receive the screen did connect.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil];
Then you need to keep a reference to your separate window and push controllers to it as you would any other window.
- (void) myScreenInit:(UIScreen *)connectedScreen:(UIViewController*)mynewViewController
{
//Intitialise TV Screen
if(!windowTV)
{
CGRect frame = connectedScreen.bounds;
windowTV = [[UIWindow alloc] initWithFrame:frame];
windowTV.backgroundColor = [UIColor clearColor];
[windowTV setScreen:connectedScreen];
windowTV.hidden = NO;
}
UIViewController* release = windowTV.rootViewController;
windowTV.rootViewController = mynewViewController;
[release removeFromParentViewController];
[release release];
}
- (void)setTvController:(UIViewController*)mynewViewController
{
UIViewController* release = windowTV.rootViewController;
windowTV.rootViewController = mynewViewController;
[release removeFromParentViewController];
[release release];
}
- (void)screenDidConnect:(NSNotification *)notification {
[self myScreenInit:[notification object]];
}
There appears to be a bug in iOS 5.0 which makes this tricky. You have to enable mirroring from the running task bar (scrolling all the way the left before a second screen is detected via the API. I've posted details in my question here: How to use iOS 5+ AirPlay for a second screen
I am adding an MPMoviePlayerController to a view like so:
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
player.controlStyle = MPMovieControlStyleNone;
[player.view setFrame:self.playerView.bounds];
[self.playerView addSubview:player.view];
self.playerView is a small view inside my main view and I have custom buttons that control playback within that same main view. This all works fine.
I have a fullscreen button that works like so:
- (IBAction) btnFullScreenPressed:(id)sender {
[player setFullscreen:TRUE animated:TRUE];
[player setControlStyle:MPMovieControlStyleFullscreen];
}
This works fine, but then when I hit the Done button on the full screen controls, the movie stops playing but does not return to the smaller self.playerView in my view. How can I get it to "un-fullscreen" and return to the smaller self.playerView?
Thanks.
Quite unintuitively you actually have to set the control style to default, i.e.:
- (IBAction) btnFullScreenPressed:(id)sender {
[player setFullscreen:TRUE animated:TRUE];
[player setControlStyle:MPMovieControlStyleDefault];
}
and, of course, then set it back to none when you receive MPMoviePlayerWillExitFullscreenNotification or MPMoviePlayerDidExitFullscreenNotification (I prefer "did exit").