Control done button - iphone

I have a view with some objects, one of them is a webview where I play a video, this view change the size and the position of the object when the device rotates. The problem occurs when I am playing the video in full screen, if I start watching the video in landscape and while I am watching the video (full screen) I rotate the device and then I hit the done button when I return to the view the objects are not in the position that they should be in that orientation.
I set the position in a function, I call that function in viewdidload and also in willAnimateRotationToInterfaceOrientation but how can I control the rotation while I am watching the video?

You could do something like this:
-(void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerPlaybackDidFinish:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
[self resizeSubviews];
}
So, when the playback finishes, you call your method to resize your subviews, and by the time the player closes, your view should look correct for the orientation.
You could also register for MPMoviePlayerWillExitFullscreenNotification instead, if that more properly fits your needs.

Related

iOS presentMoviePlayerViewControllerAnimated can't be called twice using the same viewController

So I'm trying to present a MPMoviePlayerViewController using the method presentMoviePlayerViewControllerAnimated in my own view controller.
However, once I clicked the blue Done button on the top left, when I present the same viewController again (so that I can resume a video without having to create another instance of MPMoviePlayerViewController), the controls will no longer work if you let them disappear on their own after a few seconds. The controls will still function if you interact with them before they disappear, but once they fade away on their own, you can no longer bring them back up when tapping the video. This happens on both the device and the simulator.
Simplified code:
- (void) startPlayback
{
if (self.videoViewController == nil)
{
self.videoViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.videoUrl]];
}
// present the video player
[self presentMoviePlayerViewControllerAnimated:self.videoViewController];
}
assume that startPlayback is a delegate method or something in a view controller, and it's being triggered by a button. Self would refer to the view controller in this case.
So with this, it will bring up the video player which works perfectly. Then, once I wait a bit, the controls will disappear and I can bring them back by tapping the video. I can then dismiss this video player using the Done button on the top left. Once I dismiss it, and I can then bring back the video view controller. The video will still be playing, but now, when I wait a bit and the controls disappear, I can no longer bring up the controls by tapping the screen, effectively trapping myself in the video player.
I have found various solutions into hooking into the Done button, but it hasn't really allowed me to reuse the video player. For example:
// Remove the observer so that the blue button doesn't close the viewController
[[NSNotificationCenter defaultCenter] removeObserver:self.videoViewController name:MPMoviePlayerPlaybackDidFinishNotification object:self.videoViewController.moviePlayer];
// Add itself as an observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieEnded:) name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoViewController.moviePlayer];
In my movieEnded:, I can then dismiss the movie view controller via:
[self dismissMoviePlayerViewControllerAnimated];
But when I try to present the viewController again, the video player just gets stuck Loading... forever.
Any ideas?

Playing subsequent movies in one MPMoviePlayerViewController

I am having a difficulty with the MPMoviePlayViewController.
I insatiate the controller, assign the url and show the player using:
[self presentMoviePlayerViewControllerAnimated:[appDelegate movieController]];
Then when the movie finished I dismiss it:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self dismissMoviePlayerViewControllerAnimated];
The movie player is alloc in my AppDelegate.
There are some other listeners on MPMoviePlayerPlaybackDidFinishNotification. This is for the case where there might e.g. only be audio and it shows a play/pause button and counters in table cells.
The problem is that when I load a second movie in the same MPMovieViewController, it appears fine but the controls are not working correctly. They are work as long as they are visible, but as soon as they disappear there is no may of getting them back and therefore to dismiss the movie player.Sometimes closing and opening the App works, but sometimes it doesn't and I need to 'kill' the App in order to be able to start again.
Is there a way to play subsequent video's in the MoviePlayer while the controls still work?
Any suggestions how to 'reset' the Player in a way that I can prevent the other listeners from given a DEALLOC as they are listening for the action?
you dont need to dismiss the player you just need to set the new url... or maybe I missunderstood your problem...
Try adding this to your setup movie player controller
moviePlayerController.view.userInteractionEnabled = YES;

View sizing after playing movie in fullscreen mode

I have a fairly standard navigation-based iPhone app I'm developing, where on some screens, there is a button that will play a movie using an MPMoviePlayerController in fullscreen mode. There is a navigation bar at the top of the view. I'm seeing some odd behaviour with the view sizing after the movie plays. Was wondering if anybody else has seen this, and whether there is a fix. Here is what I'm seeing:
I hold the phone in one orientation (portrait or landscape)
I start the movie (it plays in fullscreen mode)
I tap the screen to hide the movie playback controls
I rotate the phone to another orientation (portrait to landscape, or vice-versa)
I tap the screen to bring back playback controls, and then tap the 'Done' button
At this point, the original view comes back on screen, in the new orientation. However, the view is not sized properly to accomodate the status bar at the top of the screen. The status bar obscures part of the navigation bar. Rotating the phone at this point corrects the problem.
If I follow the same sequence, but without hiding the playback controls, everything works just fine, and the original view is sized properly when I dismiss the movie.
Anybody have any clue what might be causing this?
Also, this seems to be pretty easy to reproduce. I'm using Xcode4.2 and the 5.0 SDK. If you create a new iPhone-only project using the "Master-Detail Application" template, and make these changes, you can see the problem happen:
First, add the following code to MasterViewController.m.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
- (void) playStory
{
NSURL *movieURL = [NSURL URLWithString:#"http://double-apps.com/thisisatest.m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
Then, replace the contents of the didSelectRowAtIndexPath method with [self playStory]:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self playStory];
}
That's it, run the app, hit the 'Detail' entry in the main screen, then follow the sequence listed above.

iphone: rotation in mpmovieplayercontroller?

I have a small view on top of an mpmovieplayercontroller. When it is not fullscreen, I am able to adjust the frame of the view to the orientation (when the device rotates). But when I enter fullscreen mode, alothough I manage to present the view, I'm no longer able to maintain the correct frame when the device rotates. It looks like in fullscreen mode, the system simply using CGAffineTransformRotate on the status bar and the moviePlayer. How can I apply this CGAffineTransformRotate to rotate my view correctly?
EDIT:
Ok, so I updated the code to rotate 90% in the X axis after a change in orientation of the mpmovieplayercontroller but the view simply disappears after the first rotate. Here is my code:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
float angle = M_PI / 2; //rotate 180°, or 1 π radians
theView.layer.transform = CATransform3DMakeRotation(angle, 1.0, 0.0, 0.0);
[self changePositionBasedOnOrientation:toInterfaceOrientation]; //here we change the frame of the view
}
I'm not sure if this would necessarily be the correct way to do it (I guess you're adding a subview to the MPMoviePlayerController view?), but what you seem to be after is a callback to when the movie player rotates in fullscreen so you can adjust your own custom views.
You could register for rotation notifications on your custom view, which can then adjust itself every time it receives a callback. You register for rotation notifications as follows:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(layoutViewForOrientation:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
You should remember to endGeneratingDeviceOrientationNotifications and remove your notification observer when you're done with the fullscreen mode.
Ok,
So the best way to overlay the mpmovieplayercontroller in fullscreen is to it like that:
[[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview:mySubview]
It's a bit hacky, but legitimate. It gives you an easy way to deal with rotations (instead of using transformations).

Change view layout with orientation

I am working on an application where I want the view to change quite a lot when the device is rotated. I know that in Interface Builder it is possible to change the orientation and set up the layout there, however when I then rotate it to portrait, the view is the same as for the landscape. Is there a way to set up the two views independently, perhaps using two nibs?
Cheers,
JP
If you're after switching view controllers on orientation changes, like when the iPod app shows it's coverflow view, the following post will help:
http://rndnext.blogspot.com/2009_06_01_archive.html
I don't know if you can do it in IB, but I know you can do it in code. You need to observe the UIDeviceOrientationDidChangeNotification Notification:
(in viewDidLoad...)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(updateViewOnRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
....
-(void) updateViewOnRotate:(NSNotification *notification) {
// update the views
}