PageViewController with MPMovieViewController - objective-c

i'm developing an iOS app for the iPad and used the PageView template. I added some buttons which play some video files. Everything works so far, but the problem is that the touch gestures get called for both views.
My view architecture looks like this
I create a MPMovieViewcontroller, set fullscreen mode and add the view to my pageview:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
mediaView = [[MPMoviePlayerViewController alloc] initWithContentURL:mediaURL];
mediaView.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
mediaView.moviePlayer.fullscreen = YES;
mediaView.moviePlayer.view.exclusiveTouch = YES;
[mediaView shouldAutorotateToInterfaceOrientation:YES];
[mediaView setWantsFullScreenLayout:YES];
[mediaView.moviePlayer prepareToPlay];
[mediaView.moviePlayer play];
[self.view addSubview:mediaView.view];
the problem is that if I try to control the volume slider, that gesture turn the pages of the superview of my MPMovieViewController. How can I avoid this?

I ran into the same problem and I ended up removing the UIPageViewController gestures from the main view and then readded them when I was done. In my case, I'm showing a toolbar on the screen when someone single taps on the page view controller and then it fades out going back to the page view controller. To allow taps on the toolbar, I did the following:
// Remove the page controller gestures from the view
for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
[self.view removeGestureRecognizer:gesture];
}
Where self is my extended UIPageViewController and I'm doing this in the method that shows something on the screen. It's going to be a little different in your case, but effectively this should work for you!

Related

MPMoviePlayerViewController not playing on 2nd run

I created a MPMoviePlayerViewController which plays a live video. However, if I play the video twice meaning opening the player, clicking done, and playing the stream again. The result is only a black screen with no controls of the MPMoviePlayerViewController. And I need to stop the simulator cause I think the application is crashing. Here's how I did it
- (void) playUrl:(NSURL *)movieInfo
{
NSURL *streamUrl = movieInfo;
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:streamUrl];
[[mpvc view] setFrame:self.view.bounds];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[mpvc.moviePlayer setShouldAutoplay:YES];
[mpvc.moviePlayer setFullscreen:NO animated:YES];
[mpvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[mpvc.moviePlayer setScalingMode:MPMovieScalingModeNone];
[mpvc.moviePlayer setUseApplicationAudioSession:NO];
[self presentMoviePlayerViewControllerAnimated:mpvc];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[player stop];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player.view removeFromSuperview];
NSLog(#"stopped?");
}
I see that in your movieFinishedCallback: implementation, you remove the MPMoviePlayerController view, but in your playUrl: implementation, you are only setting the view's frame, presumably after you have already added the view in viewDidLoad.
One obvious change which is worth trying, is update you code to use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit. According to the MPMoviePlayerViewController docs, it is deprecated as of iOS 9:
The MPMoviePlayerViewController class is formally deprecated in iOS 9. (The MPMoviePlayerController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.
Try moving the line where you add the view to the hierarchy, to the playUrl: method. Generally, it is good practice to have countering implementations in opposing methods for your event counterparts. For instance, implement a method to build and add a view when an event starts, and have a corresponding method where you tear down and remove the same view when the same event ends. But, I say 'generally' because there are always exceptions, and you may have very compelling reasons for not doing so. So, in this case, the opposing calls are presentMoviePlayerViewControllerAnimated: and dismissMoviePlayerViewControllerAnimated:, available from the UIViewController category.
After changing the view access to using dot-notation, to be consistent with your callback implementation, here is what your new playUrl: implemntation would look like, assuming you're adding the view to self.view:
- (void) playUrl:(NSURL *)movieInfo
{
NSURL *streamUrl = movieInfo;
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:streamUrl];
[mpvc.view setFrame:self.view.bounds];
[self.view addSubview:mpvc.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[mpvc.moviePlayer setShouldAutoplay:YES];
[mpvc.moviePlayer setFullscreen:NO animated:YES];
[mpvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[mpvc.moviePlayer setScalingMode:MPMovieScalingModeNone];
[mpvc.moviePlayer setUseApplicationAudioSession:NO];
[self presentMoviePlayerViewControllerAnimated:mpvc];
}
Another option is to simply not remove the player's view in your callback method. If that is not the culprit, then the next thing I would investigate is check if you are sending messages to nil objects. Also, see what happens when you take out all the implementation from movieFinishedCallback:, except for getting and stopping the player.
I hope that helps!
Fixed the issue by removing the [player.view removeFromSuperview] line

youtube full screen video not calling viewWillAppear or viewWillRotate

So I have a UIWebView in which sometimes it has a youtube embedded player in it. When I play it and go to full screen and rotate and then dismiss the video, it doesn't call the viewWillAppear or the willRotateTo.... why is this?? I need to make some view adjustments when the device is rotated, however when the player is presented, for some reason none of these methods are being called. And yes I have shouldAutoRotateToInterfaceOrientation set correctly. Any idea?
You can use following for your UIwebView problem, viewWillAppear or willRotateTo.. never calls on UIWebView. You can detect the end of full screen mode by observing the #"UIMoviePlayerControllerDidExitFullscreenNotification" mode:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidExitFullscreen:)
name:#"UIMoviePlayerControllerDidExitFullscreenNotification"
object:nil];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)moviePlayerDidExitFullScreen:(NSNotification *)notification
{
// This is where you do whatever you want.
}
Your controller must be a UIViewController or the viewWillAppear delegate won't be called.

Unbalanced calls to begin/end appearance transitions

I'm currently pulling my hair out solving this bug :/ I have already tried the solutions from other SO threads regarding this topic but had no luck so far.
Here's what's wrong:
I have a UINavigationController that pushes View A, from View A I can press a button to push View B - works fine. But when I push View B, then rotate the screen into landscape mode and then click the back button, I get the following output in the console and the view switching is not animated, just switches from B back to A:
2012-01-02 20:50:42.866 [13345:f803] Unbalanced calls to begin/end appearance transitions for <DimensionConversionViewController: 0x68831f0>.
2012-01-02 20:50:42.868 [13345:f803] attempt to dismiss modal view controller whose view does not currently appear. self = <UINavigationController: 0x6b541a0> modalViewController = <UISnapshotModalViewController: 0x6da5190>
This is how I push the View B into the stack:
- (void) showConverter:(id)sender {
[self.navigationController pushViewController:converter animated:YES];
}
-viewDidLoad of View B:
- (void) viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateInterface) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
// ... Update text fields ...
[self updateInterface];
}
-viewDidUnload of View B:
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
If you have questions or need more code samples, please let me know.
Thanks in advance for any help :-)
Turned out that in my case the root cause of the problem was, that I forgot to update all the shouldAutorotateToInterfaceOrientation: methods in the different view controllers to return YES for all UIInterfaceOrientations (or let's say they should all return the sam). Doing this solved the issue.

Refresh UIView from Subview

in my universal app, I have a UIControl View inside of a UIScrollView.
On pressing a setup Button, I add another View as subview, like this:
SetupController *setupview = [[SetupController alloc] initWithNibName:#"SetupView-iPad" bundle:nil];
[mainview addSubview:setupview.view];
The subview is displayed like expected.
In this SubView I have some Buttons, which allows the user to switch between settings.
The performed actions are saved in a local Database.
The problem is: On ButtonClick in the SubView, I have to refresh the mainview, to apply the changes. I've tested many ways to make this happen:
In the Subviews class:
[self.parentViewController.view setNeedsDisplay];
No result.
Then I tried to refresh the mainview by notification:
I added this to my subviews classfile, in the function that changes my settings.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:#"setEmoticon_NOTIFICATION" object:self];
}
Then I added the observer to my mainview in the ViewDidLoad method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshView) name:#"setEmoticon_NOTIFICATION" object:nil];
and created the function for this observer:
-(void)refreshView{
NSLog(#"Notification!");
[self.view performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
}
In Log I get the "Notification!" on changing the settings. But whatever I try, no refresh.
I tried setNeedsDisplay, resignFirstResponder, [self viewDidLoad:] , but still nothing works.
Any ideas how to refresh my mainview?
What exactly do you need to do when refreshing? Do you need to redraw elements on screen, etc?
Do you have a custom refresh method you've implemented in the main view? I would setup a custom protocol in your sub view and before adding the sub view to the main view, I would add the sub view's delegate as the main view
SetupController *setupview = [[SetupController alloc] initWithNibName:#"SetupView-iPad" bundle:nil];
setupView.delegate = mainview;
[mainview addSubview:setupview.view];
Then handle the refresh in the main view call back.
Hell yeah, seems like I found a solution to refresh my superview from subview.
You only have to add the following code snippet to the desired action/method, that should perform the refresh, in your subview controller.
for (UIView* next = [self.view superview]; next; next = next.superview)
{
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
{
[(UIViewController*)nextResponder viewWillAppear:YES];
}
}
In this case I reload the superview, using it's viewWillAppear method.
You can replace it with any method that fits your needs (viewDidLoad, viewWillDissapear etc.).
It may not be the best way to do this, but it works great for me.
Thanks for your answers. ;)

MPMoviePlayerViewController doesn't show well when playing video

I trying to show video on my App. The App hides the iPhone top panel. The player seems to work fine. There is just one annoying problem: when the player shows the video, it sometimes show the top panel and sometime hides it. When it is hidden, the video player panel is pushed a little (the same size of the panel that used to be there). Is is Apple bug? Am I doing something wrong?
Here is my code:
- (void) showFullscreenMediaWithURL: (NSURL *) mediaURL
{
MPMoviePlayerViewController *ctrl = [[MPMoviePlayerViewController alloc] initWithContentURL: mediaURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector: #selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:ctrl.moviePlayer];
ctrl.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
ctrl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[ctrl setWantsFullScreenLayout:YES];
[self presentMoviePlayerViewControllerAnimated:ctrl];
[ctrl release];
}
-(void) playbackDidFinish:(NSNotification*)aNotification
{
NSLog(#"Finished playback");
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self dismissMoviePlayerViewControllerAnimated];
[[captureManager session] startRunning];
}
if by iPhone top panel you mean the iPhone Status bar, then the solution should be simple.
Just before present/dismissMoviePlayerViewControllerAnimated add the following:
// Hide Status Bar
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
// Show Status Bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
Update: I can see what seems to be your problem.
First, the upper bar with the network indication icons and other information is the status bar (and nothing else). Your problem seems to be more ViewController related then a MediaPlayer. In other words, If you would have try to "push" some other ViewController to full screen (as the player is) you would have experience the exact same issue.
Second, the proper way, or I might say: my preferred way, of loading a view controller to full screen is the following:
Setup a full screen rootViewController which will be loaded on applicationDidFinishLaunchingWithOptions on your appDelegate.
On the rootViewController init put your default viewController (the one you used to load from appDelegate). Make sure that the rootViewController.view's frame is filling the screen.
Create 2 messages on rootViewController: LoadFullscreen:viewController and dismissFullscreen using present/dismissModelViewController. the setStatusBarHidden messages should be called from here.
To Lunch the player on full screen, create the player viewController and perform [rootViewController LoadFullscreen:player];
There are some other ways, but generally, this is the best practice and the method I recommend. It's relatively "a lot of code" to implement, thats why I couldn't help you with code snippers, but the general idea is relatively simple.
I hope that's help, E.G :)