MPMoviePlayerController iPhone Navigation Bar Problem - iphone

I have a view controller which includes a MPMoviePlayer inside. I hide the navigation bar, and want it to be shown when the video paused.
When the video is loaded, it works just fine. After i paused the video navigation bar appears but it pushes the whole player view to a little bit down. How can i make navigation bar appear without affecting the player view.
here is the code i used:
In ViewDidLoad():
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerPlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[self setWantsFullScreenLayout:YES];
[moviePlayer prepareToPlay];
//For viewing partially.....
moviePlayer.view.backgroundColor = [UIColor blackColor];
//[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 410)];
[moviePlayer.view setFrame:[self.view bounds]];
moviePlayer.fullscreen = YES;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
- (void) videoPlayerPlaybackStateChanged:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
if(player.playbackState == MPMoviePlaybackStatePaused){
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationController.navigationBarHidden = NO;
}
else if(player.playbackState == MPMoviePlaybackStatePlaying){
self.navigationController.navigationBarHidden = YES;
}
[player autorelease];
}
Thank you in advance...

In your condition
if(player.playbackState == MPMoviePlaybackStatePaused)
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationController.navigationBarHidden = NO;
}
Try using
[self.navigationController.navigationBar setTranslucent:YES];

Related

how to provide only one view with landscape for MPMoviePlayerController in IOS6

i had spend 2 hours finding the correct code to fix my orientation problem
this are my movieplayer code. i need this particular view to be shown in landscape.
in appdelegate i set all orientation and in my rootviewcontroller i set is as portrait only and in my movieplayer view as landscape but not luck. can anybody give me some comment on how to fix the issues please ?
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAll;
}
my rootviewcontroller
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)prepareIntroVideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"designthinking_pt1" ofType:#"mp4"]];
self.playercontroller = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.playercontroller.view setFrame:CGRectMake(0, -20, 320, 480)];
self.playercontroller.movieSourceType = MPMovieSourceTypeFile;
self.playercontroller.scalingMode = MPMovieScalingModeAspectFill;
self.playercontroller.fullscreen = NO;
self.playercontroller.controlStyle = MPMovieControlStyleFullscreen;
//playercontroller.controlStyle = MPMovieControlStyleFullscreen;
self.playercontroller.view.userInteractionEnabled =YES;
self.playercontroller.view.backgroundColor = [UIColor blackColor];
self.playercontroller.shouldAutoplay = NO;
//playercontroller.repeatMode = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playercontroller];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground)
name: UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
[self.playercontroller prepareToPlay];
[self.view addSubview:self.playercontroller.view];
[self.playercontroller setFullscreen:YES animated:YES];
//[self.playercontroller stop];
[self.view sendSubviewToBack:self.playercontroller.view];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft;
}
You can check my example... https://dl.dropboxusercontent.com/u/19438780/testRotate.zip
I have 2 controllers with navigation controller - portrait; for this I "subclassed" nav controller (here is the "rotation" code for portrait)
The 3th controller is landscape - must be presented modal if you want to rotate it - put the rotation code accordingly .
I would look into preferredInterfaceOrientationForPresentation, I spent about a week on this problem a few months ago. I don't remember exactly how i solved it but I do know it had something to do with implementing that method

Resize the MPMoviePlayerController dynamically

How can I resize the MPMoviePlayerController dynamically to fix all the size of screens for iPhone/iPad (both portrait & landscape)?
this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithTitle:#"About" style:UIBarButtonItemStyleDone target:self action:#selector(openPopup)];
self.navigationItem.leftBarButtonItem = about;
NSURL *streamURL = [NSURL URLWithString:#"http:/streamUrl/playlist.m3u8"];
_streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
self.streamPlayer.controlStyle = MPMovieControlStyleEmbedded;
[self.view addSubview: self.streamPlayer.view];
[self.streamPlayer play];
}
I also tried to add
[self.streamPlayer.view setFrame:self.view.bounds];
but this makes the video is not fix to screen as shown in screenshot
How can I make it fix all screens of all different devices?
Thanks in advance.
The best solution I found is to use the following
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
[self.streamPlayer.view setFrame:CGRectMake(10, 10 ,height-20, width-70)];
i solved this problem in ios 5
as:
AvPlayerLayer *avPlayerLayer =[[AVPlayerLayer alloc]init];
avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
avPlayerLayer.frame = self.playerView.layer.bounds;
avPlayerLayer.transform = CATransform3DMakeRotation(M_PI / 2.0, 0, 0, 1);
[self.playerView.layer addSublayer:avPlayerLayer];
here is avPlayerLayer.transform you can set it in any direction .
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];
if (moviePlayerViewController) {
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
[moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
if ([moviePlayerViewController.moviePlayer respondsToSelector:#selector(setAllowsAirPlay:)]) {
[moviePlayerViewController.moviePlayer setAllowsAirPlay:YES];
}
[[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController queue:nil usingBlock:^(NSNotification *notification) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dismissMoviePlayerViewControllerAnimated];
}];
[moviePlayerViewController.moviePlayer play];
}

How to add an Subview to a Fullscreen MPMoviePlayer

I want to add a Logo (UIImageView) to a fullscreen MPMoviePlayerController.
It works fine when the MPMoviePlayerController is not in fullscreen Mode. When i switch to full screen all added views on the MPMoviePlayerController removed(hidden).
Here is my code:
- (void)setupMPMoviePlayer{
[self.moviePlayer.view removeFromSuperview];
[self removeNotifications];
self.moviePlayer = nil;
self.moviePlayer = [[MPMoviePlayerController alloc] init];
[self addNotifications];
[self.moviePlayer.view setFrame:self.view.bounds];
self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.moviePlayer.fullscreen = NO;
[self.view addSubview:self.moviePlayer.view];
if ([self.moviePlayer respondsToSelector:#selector(setAllowsAirPlay:)])
[self.moviePlayer setAllowsAirPlay:YES];
[self.moviePlayer setContentURL:[NSURL URLWithString:urlString]];
[self.moviePlayer setFullscreen:YES animated:YES];
[self addLogoViewAtView:self.moviePlayer.view];
}
- (void)addLogoViewAtView:(UIView *)view{
UIView *theView = [view viewWithTag:101];
if (theView.superview) {
[theView removeFromSuperview];
}
UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo_weiss.png"]];
logoView.tag = 101;
logoView.frame = CGRectMake( logoView.image.size.width - 100.0,
100.0,
logoView.image.size.width,
logoView.image.size.height);
logoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
[view addSubview:logoView];
[view bringSubviewToFront:logoView];
}
Now my question is there an alternate way to add an subview to MPMoviePlayerController?
I can add the logo to the application's window but this ist not so clean.
Anyone have any idea on how I could do this?
Most propably fullscreen mode add MPMoviePlayerController view at keyWindow for an application or present a modal view controller above. So, you can try looking for the view in window.subviews or modal view controllers for moviePlayer.

how to play a video on startup of the app or on viewdidload with done button?

I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.

MPMoviePlayerController with UIPopoverController

I am using the MPMoviePlayerController to play a video on UIPopoverController.
When UIPopoverController dismiss video keeps on playing in background. Is there any way to stop and release MPMoviePlayer.
In my code There is FirstViewController and ViewVideoController which has function:
#implementation FirstViewController
- (void)popOverViewDisplay:(id)sender {
//if(![popoverController isPopoverVisible]){
NSLog(#"my popover....");
ViewVideoController *videoController = [[[ViewVideoController alloc] initWithNibName:nil bundle:nil] autorelease];
videoController.contentSizeForViewInPopover =CGSizeMake(550, 460);
popoverController = [[UIPopoverController alloc]
initWithContentViewController:videoController];
popoverController.delegate = self;
[videoController release];
popViewBtnFrame = CGRectMake(299, 357, 63, 42);
[popoverController presentPopoverFromRect:popViewBtnFrame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
if (self.popoverController != nil) {
[self.popoverController dismissPopoverAnimated:YES];
//[self.popoverController release];
NSLog(#" if loop popover dismissed");
ViewVideoController *videoController = [[ViewVideoController alloc] initWithNibName:#"ViewVideoController" bundle:nil];
[videoController unloading];
}
NSLog(#"popover dismissed");
}
on button press player start playing in popoverController.
#implementation ViewVideoController
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"AlphabetTrain" ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
//player.movieControlMode = MPMovieControlModeHidden;
player.controlStyle = MPMovieControlStyleDefault;
player.view.frame = CGRectMake(75.0f, 80.0f, 400.0f, 300.0f);
[[self view] setCenter:CGPointMake( [[self view] bounds].size.width / 2, [[self view] bounds].size.height / 2)];
[self.view addSubview:player.view];
[player play];
//[player release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[super viewDidLoad];
}
- (void)myMovieFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerController* player1 = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player1];
NSLog(#"stop player");
[player stop];
}
- (void)unloading { //this function called in firstviewcontroller to stop player on dismiss
NSLog(#"unloading player");
//[player endSeeking];
[self.player stop];
}
When I dismiss ViewVideoController player keeps on playing. Is there any way to stop/release player on dismiss of ViewVideoController
You can use delegate method
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[moviePlayer stop];
}