MPMoviePlayerViewController doesn't show well when playing video - objective-c

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 :)

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

Using one ActivityIndicator with two classes

I have an UIActivityIndicator that starts animating on the top of all of my views. Right after he starts animating, the parent view of this current view is popped. Now, after he's animating, I'm calling another class with a block and runs some server commands.
My problem is, that in the other class im getting the response from the server, but I cannot tell the
UIActivityIndicator to stop, because he is in the other class. (I have to say that I don't want to implement nothing on the Application Delegate).
On the server class, after I get the response, a UIAlertView appears, but the UIAlertView is implemented inside the server class. That's where I want the UIActivityIndicator to stop.
I Hope that I understand it well, if not, please tell me.
Thank you.
- (void)buttonPressed:(id)sender
{
UIView * darkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
darkView.backgroundColor = [UIColor blackColor];
darkView.alpha = 0.5f;
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[darkView addSubview:activityIndicator];
activityIndicator.center = darkView.center;
[activityIndicator startAnimating];
[[UIApplication sharedApplication].keyWindow addSubview:darkView];
// Inside this class (ShareByEmail) there is a UIAlertView that should stop the
// animation that already running right now.
ShareByEmail *sbe = [[ShareByEmail alloc]init];
[sbe share];
[self.navigationController popViewControllerAnimated:YES];
}
One option is to keep the UIActivityIndicator as singleton object and use it from anywhere in the project. Another option is to try this with notifications. You need to add and remove observer to this activity indicator and whenever a request is fired/executed, you need to post a notification to start/stop activity indicator.
Update:
In your case either you can set it as [[NSNotificationCenter defaultCenter] addObserver:activityIndicator selector:#selector(startAnimating) name:#"startActivityIndicator" object:nil] and [[NSNotificationCenter defaultCenter] addObserver:activityIndicator selector:#selector(stopAnimating) name:#"stopActivityIndicator" object:nil] immediately after allocating memory for the activity indicator. Now whenever you want to start or stop it, call [[NSNotificationCenter defaultCenter] postNotificationName:#"startActivityIndicator" object:nil] or [[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil]. Make sure that the activity indicator is not released. I would suggest you to declare your activity indicator as a class level variable in this class and allocate memory in init method or so. In your button pressed method, you can just use [darkView addSubview:activityIndicator];

UIWebView scrolling down on input focus iOS 6 only

I'm loading a UIWebView in a modal. The web page has some inputs that work exactly as intended in iOS 5. However in iOS 6, anytime an input gets focus, the keyboard is doing it's automatic 'centering' of the form even though the inputs have plenty of room to be shown above the keyboard. Since some of the inputs are at the top of the page, this forces them out of view so the user cannot see what they are typing. Scrolling back up to see the inputs causes the keyboard to stop working until dismissed and refocusing the input (in turn scrolling it out of view again). Is this an expected behavior in iOS 6? Is there any way to keep the webview from scrolling when an input gains focus (like how iOS 5 works)?
Without knowing the inner workings of your code, I'm going to try to help as much as I can. If nothing else, I hope to provide some food for thought. You asked two questions:
Is it expected behavior in iOS6?
What you're seeing is quite odd for sure. It's strange that the webView is centering the form rather than centering the input field. And it should definitely not cause the active input field to scroll out of view. Furthermore, the keyboard seems to stop working, which is quite odd. However, it is expected to see different behavior in iOS 5 and 6 with regards to the webView scrolling. As you said, iOS 5 scrolls the inputField into view while iOS6 puts it in center.
Is there any way to keep the webview from scrolling when an input gains focus (like how iOS 5 works)?
Yes. I provide code to do just this below -- namely to stop the webView from scrolling. If that is exactly what you want to do, then great. However, stopping the webView from scrolling is not the same as getting the same behavior as iOS5. Still, I wanted to give you this option as you requested it.
#import "ViewController.h"
#interface ViewController () <UIScrollViewDelegate> {
CGPoint origin;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView loadRequest:request];
self.webView.scrollView.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
// NOTE THAT SIMPLY DISABLING THE SCROLL WILL NOT PREVENT KEYBOARD SCROLL
//[self.webView.scrollView setScrollEnabled:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)keyboardWillShow {
NSLog(#"keyboard");
origin = self.webView.scrollView.contentOffset;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"scroll");
[self.webView.scrollView setContentOffset:origin];
}
#end
If you want consistent scroll behavior...
I took some time to code up this scroll modification. You can use it as a reference in making your own scroll behavior that is consistent across iOS 5&6. In this code: When the user clicks on a text input box on a webpage, the code will stop the default scrolling behavior by keeping the page scrolled to its current position. Unfortunately, there is no way to cancel all scrolling, but rather you have to override scrolls. Once the default scrolling has been "suppressed", it gets the position of the active input box and scrolls to that position. This will put the active input box in the top of the screen. You can modify this according to your preferences. For example, you can use [UIScrollView scrollRectToVisible] to make it more like iOS5.
#import "ViewController.h"
#interface ViewController () <UIScrollViewDelegate> {
CGPoint origin;
CGPoint activeElementOrigin;
BOOL pauseScroll;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView loadRequest:request];
self.webView.scrollView.delegate = self;
pauseScroll = NO;
origin = CGPointZero;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)keyboardWillShow {
NSLog(#"keyboard");
pauseScroll = YES;
origin = self.webView.scrollView.contentOffset;
}
- (void)keyboardDidShow {
NSLog(#"keyboard DID SHOW");
pauseScroll = NO;
[self.webView.scrollView setContentOffset:activeElementOrigin animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"scroll");
if (pauseScroll) {
[self.webView.scrollView setContentOffset:origin animated:NO];
NSString *javaScript = [NSString stringWithFormat:#"function f(){ var textField = document.activeElement; return textField.getBoundingClientRect().top; } f();"];
NSString *textFieldRectTop = [self.webView stringByEvaluatingJavaScriptFromString:javaScript];
activeElementOrigin = origin;
activeElementOrigin.y = [textFieldRectTop floatValue]-10;
}
}
#end
If this code helped you, it would be very nice of you to reward the bounty to me.
I would be humbly appreciative.
A workaround could be disabling the scroll in this UIWebView :
UIScrollView *scrollView = webView.scrollView;
[scrollview setScrollEnabled:NO];
[scrollView bounces:NO];
Another could be setting the UIEdgeInsets of this scrollView but I don't know much about this.
I think this is a standard behaviour in both iOS 5 and 6 to scroll to make the input field just on top of te keyboard (just like Safari does).
The checked answer on this post has your answer:
How to make a UITextField move up when keyboard is present?
In short, you need to move up the view containing the inputs which are being pushed out of view. The post also contains sample code on how to do it.
Hope this helps.

PageViewController with MPMovieViewController

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!

Cannot change views in splitviewcontroller

I am trying to change the views in a splitview controller based upon clicking a button in a modalview (a person is selecting an option). I am using notifications to accomplish this:
When the button is clicked in the modal view, it issues a notice, then closes (dismisses) itself:
[[NSNotificationCenter defaultCenter] postNotificationName:#"launchProject" object:nil];
The DetailViewController inside the split view controller is listening for this notification, and switches out the views in the SVC
-(void)launchProject:(NSNotification *)notification {
Project* secondDetail2 = [[Project alloc] initWithNibName:nil bundle:nil];
ProjectRootController* secondRoot2 = [[ProjectRootController alloc] initWithNibName:nil bundle:nil ];
self.splitViewController.viewControllers =[NSArray arrayWithObjects: secondRoot2, secondDetail2 , nil];
}
I don't understand why the views aren't switching out. Any advice on this will be welcome.
You haven't shown all the code, so I'm guessing the problem is a misunderstanding of how notifications work. It can be initially confusing, but it's very straightforward. So far, you have:
[[NSNotificationCenter defaultCenter] postNotificationName:#"launchProject" object:nil]
which is fine.
But you also need to have
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(launchProject:) // selector should be your function name, launchProject
name:#"launchProject" // notification name - must be same as what is given to postNotificatioName.
object: nil];
somewhere, like in an init function.
In other words, postNotificationName:#"launchProject" does NOT call your function launchProject. It puts a notification with the name "launchProject" into the NSNotificationCenter defaultCenter. If you're not looking for that particular notification, then nothing will happen.
Hope that helps..