View video in Landscape mode - xcode

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;
}
}

Related

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;
}

IOS allocation problems with MPMoviePlayerViewController

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];
}

How do I stop a sound when a new sound button is pressed

I'm new to Xcode and I'm having an issue, if you click the sound buttons multiple times they overlap each other. How would I set my code so that if you click a button while a sound is playing it will stop the current sound playing and start the new one.
If you could post the code it would be much appreciated.
Thanks,
Dominick
Here is a copy of my current code:
#import "Sound3ViewController.h"
#implementation Sound3ViewController
-(IBAction)playnow:(id)sender;{
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"winning"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)play2:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"onadrug"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (void)dealloc
{
[super dealloc];
[sound release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would suggest you to disallow playing the sound with a variable and schedule an action that allows it again after the duration of the file. Eg:
boolean allowed = true
-(IBAction)playnow:(id)sender
{
if (allowed) { // <--- ADD THIS
allowed = false;
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"winning"
ofType:#"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
NSInvocation *invocation = [[NSInvocation alloc] init];
[invocation setTarget:self];
[invocation setSelector:#selector(allowPlaying)];
[NSTimer scheduledTimerWithTimeInterval:[sound duration] invocation:invocation repeats:NO];
} // <-- AND THIS
}
-(void)allowPlaying
{
allow = true;
}
Didn't test it since just wrote on the fly but you've got the general idea..
I'm new to objC also, so I'm not sure this is correct, but try this:
#import "Sound3ViewController.h"
#implementation Sound3ViewController
-(IBAction)playnow:(id)sender;{
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"winning"
ofType:#"mp3"]];
// NEW STUFF
if (sound) {
if ([sound playing]) {
[sound stop];
}
[sound release];
}
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)play2:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"onadrug"
ofType:#"mp3"]];
if (sound) {
if ([sound playing]) {
[sound stop];
}
[sound release];
}
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (void)dealloc
{
[super dealloc];
if (sound) {
[sound release];
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
// in .h file
#import "AVFoundation/AVAudioPlayer.h"
//set delegate
#interface SamplesoundViewController : UIViewController <AVAudioPlayerDelegate>{
AVAudioPlayer *player1;
AVAudioPlayer *player2;
...
...
}
#property (nonatomic, retain) AVAudioPlayer *player1;
#property (nonatomic, retain) AVAudioPlayer *player2;
-(IBAction)soundfirst;
-(IBAction)soundsecond;
-(IBAction)Newmethodname;
// in .m file
#implementation SamplesoundViewController
#synthesize player1,player2;
-(IBAction)soundfirst{
NSString *path = [[NSBundle mainBundle] pathForResource:#"soundfilefirst" ofType:#"mp3"];
player1=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player1.delegate=self;
[player1 play];
[self performSelector:#selector(soundsecond) withObject:nil afterDelay:0.90];
}
-(IBAction)soundsecond{
[player1 stop];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Soundfilesecond" ofType:#"mp3"];
player2=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
player2.delegate=self;
[player2 play];
[self performSelector:#selector(Newmethodname) withObject:nil afterDelay:1];
}
-(IBAction)Newmethodname{
[player2 stop];
//write your code here
}

Resources