IOS allocation problems with MPMoviePlayerViewController - memory-management

in my ipad app in viewDidLoad i set up a video in the first view so to have an intro.
In allocation tool i see that allocated memory grow till 120MB when running the video, ok i dont care, but after the video finish i would like that memory to go to 0 again BUT stick to 120MB, what am i doing wrong ?
- (void)viewDidLoad{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"sfed"
ofType:#"mp4"];
playerViewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[videoview addSubview:playerViewController.view];
MPMoviePlayerController *player = [playerViewController moviePlayer];
player.view.frame = CGRectMake(1024, 748, 0, 0);
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[player release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1];
[videoview setAlpha:0];
[UIView commitAnimations];
[videoview release];
}

Related

Set UIView bottom space constraint to keyboard height

I'm a big fan of autolayout, but have always implemented it in Storyboard.
I have a UIView that I want the bottom space constraint to be where the keyboard ends.
I have defined all my constraints, can someone show me how to implement this constraint in code for just the bottom space? And also how to get the value of the keyboard height depending on the specific iDevice and match that to the constraint.
Thanks
Take an outlet of the bottom constraint of the view like this
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintContainerBottom;
and use this
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupKeyboard:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self setupKeyboard:NO];
}
- (void)setupKeyboard:(BOOL)appearing
{
if (appearing) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}else{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
}
- (void)keyboardWillShow:(NSNotification*)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:#"keyboardShown" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.constraintContainerBottom.constant = keyboardSize.height;
[self.view layoutIfNeeded];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
double duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
int curve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView beginAnimations:#"keyboardHidden" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
self.constraintContainerBottom.constant = 0;
[self.view layoutIfNeeded];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}

Play Landscape Video in iOS8 with Landscape Correct Statusbar Inside Portrait App

Portrait App, loads video that displays in landscape.
in iOS7 all works fine (minor fix this is iOS6 too) but in iOS8 the statusbar, although showing in landscape actually shows at the size of a portrait statusbar so it only occupies the top left hand 60% of the video (if holding device in landscape).
The code I'm using is as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
[planTitleLabel setText:[[ProgrammeHandler sharedHandler]
stringForElement:kPlanStringElementPlanTitle
onDay:0 inPlan:selectedPlan]];
}
- (NSUInteger)supportedInterfaceOrientations{
return 0;
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationNone];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0 && [[UIDevice currentDevice].systemVersion floatValue] < 9.0) {
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
}
NSString *moviePath = [[ProgrammeHandler sharedHandler] pathForAsset:kAssetVideoIntro onDay:0 forPlan:selectedPlan];
CGSize screenBounds = [[UIScreen mainScreen] bounds].size;
[whiteFadeView setFrame:CGRectMake(0, 0, screenBounds.width, screenBounds.height)];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[moviePlayer view] setBackgroundColor:[UIColor blackColor]];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
//For viewing partially.....
moviePlayer.view.transform = CGAffineTransformMakeRotation(M_PI/2);
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[movieView setAlpha:0];
[moviePlayer.view setFrame:[movieView frame]];
moviePlayer.view.backgroundColor = [UIColor blackColor];
[movieView addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(hideControl)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
[UIView animateWithDuration:0.5f delay:0.50f
options:UIViewAnimationOptionCurveEaseIn
animations:^{[movieView setAlpha:1];} completion:^(BOOL fin){ }];
}
- (void) hideControl {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:moviePlayer];
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
}
I did see this question but don't think it applies in my case.
iphone: rotation in mpmovieplayercontroller?
Does any one know how to restore the statusbar to it's fullscreen 'width'?
Ok, after looking over this again today I noticed that there was an error that iOS7 seems happy with but it was correctly throwing an error in iOS8. For anyone else with the same issue:
The fix is simply to apply the transformation to the moviePlayer itself rather than the subview. e.g.
replace
moviePlayer.view.transform = CGAffineTransformMakeRotation(M_PI/2);
with
movieView.transform = CGAffineTransformMakeRotation(M_PI/2);

Playing a video to start the application xcode

Playing a video to start the application
I have this code, I can't see the video and I can hear the audio.
Could anyone explain why ?
-(void)awakeFromNib
{
NSURL *url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tecnolitevideo_1" ofType:#"mov"]];
moviePlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url2];
[moviePlayer presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
}
-(void)moviePlayerPlaybackStateChanged:(NSNotification *)notification {
}
- (void)viewDidLoad
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"3" withExtension:#"mov"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
m_player = nil;
}
I am try this and works fine!!!

mpmovieplayercontroller disappears as soon as it starts

When I click “play movie” the mpmovieplayer presents itself for half a second and I see it’s loading movie and the controls are there, but then it returns to main screen without playing the video.
How do I fix this?
Edit:
I have changed code and now the player stays up but is stuck on loading, it doesn’t play movie.
IS the problem: not preparing the movie for play? Or not stopping movie in background and then restarting it?
#import "ViewController.h"
#implementation ViewController
#synthesize moviePlayer;
-(IBAction)grabVid:(id)sender;
{
[self presentModalViewController:imagePicker animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[imagePicker setDelegate:self];
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
imagePicker.sourceType];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
{
[imagePicker dismissModalViewControllerAnimated:YES];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
//[imagePicker dismissModalViewControllerAnimated:YES];
}
-(IBAction)playMovie:(id)sender
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.view addSubview:playercontroller.moviePlayer.view];
[playercontroller.moviePlayer setFullscreen:YES animated:YES];
playercontroller.moviePlayer.shouldAutoplay = NO;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
//[playercontroller.moviePlayer release];
}
- (void)dealloc {
//[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
-(IBAction) playVideo
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.initialPlaybackTime = 0;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
// playercontroller = nil;
}

View video in Landscape mode

I have a video in my application, but it's in portrait. I want to display it in landscape mode, but I don't have any idea how to do that.
I used this code to make my video:
- (IBAction)playMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (IBAction)playSecondMovie:(id)sender
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"00 01. Welcome" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
-(void) tableView: (UITableView*) tableView
didSelectRowAtIndexPath: (NSIndexPath*) indexPath
{
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"BAZO" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
//Uncomment om beeld formaat aan te passen
//moviePlayerController.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayerController play];
/*-(void)loadVideo:(NSString *)name ofType:(NSString *)type{
NSURL *url=[NSURL fileURLWithPath:[mainBundle pathForResource:#"BAZO" ofType:#"m4V"]]
if (!mp) mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mp play];
}*/
}
Maybe somebody can give me a hint or a method so I can put this to landscape?
I used this code:
[MPMoviePlayerController setOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
but it gives me the warning :
MPMoviePlayerController may not respond to -setOrientation:animated:
What's happening?
Had this problem as well,
I was running it in the appDelegate file, which meant I had to force the window to change to landscape so if the original is like this:
-(void)playMovie {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"intro"
ofType:#"m4v"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[window addSubview:player.view];
[window bringSubviewToFront:player.view];
[player setFullscreen:YES animated:YES];
[player setControlStyle:MPMovieControlStyleNone];
[player play];
}
all that is needed is to add these lines:
[window setBounds:CGRectMake( 0, 0, 480, 320)];
[window setCenter:CGPointMake(160, 240];
[window setTransform:CGAffineTransformMakeRotation(M_PI/ 2)];
before the [player play], and at the movie finish call, add
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
window =[[UIWindow alloc] initWithFrame:rect];
[window makeKeyAndVisible];
to reset the view.
Not sure if it's the right way to do it, but it was the only thing worked for me.
Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style, etc. I referred this answer for the same.
MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];
and in the AppDelegate.m, declare the following function which will restrict the orientation to portrait in all cases and will only change when a video is playing
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAll;
}
else
{
//NSLog(#"orientation should change");
return UIInterfaceOrientationMaskPortrait;
}
}

Resources