My code was working fine until I upgraded iPhone to iOS 5.0. The MPMoviePlayerViewController used to work fine but it doesn't work on iOS 5.0 so I have to use MPMoviePlayerController for iOs 5.0 and later versions. It works fine but MPMoviePlayerController doesn't rotate automatically like it used to do with MPMoviePlayerViewController.
Following is my code. Could anyone please suggest me how to make MPMoviePlayerController code rotate automatically?
-(void)playVideo {
NSString *filePath = [appDelegate filePath:#"startup.mp4"];
if(!appDelegate.iOS5) {
// This works perfectly till iOS 4 versions. Rotates automatically
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
[self presentMoviePlayerViewControllerAnimated:videoController];
} else {
// This doesn't rotate automatically
NSURL *url = [NSURL fileURLWithPath:filePath];
MPMoviePlayerController* moviePlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
Try subclassing MPMoviePlayerController and forcing the orientation to portrait only.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Not the best solution but I guess it should work.
Related
I am playing video in MPMoviePlayer in my app,I am fetching the video from server.
If the video length is about 25sec or more,the audio gets break.Means while playing the video when it reaches half then there is no audio can be heared.But if I seek the progess bar,audio again comes.
Can any one help me in this ?
moviePlayerController = [[MPMoviePlayerController alloc] init];
[moviePlayerController prepareToPlay];
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController setScalingMode:MPMovieScalingModeAspectFit];
[[self.moviePlayerController view] setFrame:CGRectMake(0, 72, 320,180)];
[[self view] addSubview: [self.moviePlayerController view]];
self.moviePlayerController.useApplicationAudioSession = YES;
NSString *strng = #"http://qdemo_videos.s3.amazonaws.com/1360582540.mp4";
NSURL * adurl = [NSURL URLWithString:strng];
moviePlayerController.contentURL = url;
moviePlayerController.controlStyle = YES;
self.moviePlayerController.useApplicationAudioSession = YES;
[moviePlayerController play];
If u are building on Xcode 4.6 or above and using iOS 6.0 or above then,
self.moviePlayerController.useApplicationAudioSession = YES;
will not be called as its deprecated.
For further use this link:
Here's the doc
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html
Here's the explanation http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html
And if it's not enough here is the code :) http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html#//apple_ref/doc/uid/TP40007875-CH6-SW2
In my case setting the category to AVAudioSessionCategoryAmbient did the trick!
I am trying to implement video into my app and I am having a hard time. I have tried this tutorial and it was super helpful.
But now when I try to run my app and click on the button, the video frame comes up and just stays black. My video is in the correct format, I also made sure that I am pulling in the MediaPlayer Framework. Has anyone ran into this issue before or knows why this would be happening?
This is what I have:
-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"big-buck-bunny-clip" ofType:#"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
moviePlayerController.initialPlaybackTime = 5;
[moviePlayerController play];
}
Have you added MPMoviePlayerController's view onto your view?
MPMoviePlayerController *movieController = ...;
movieController.view.frame = self.view.bounds;
[self.view addSubview:movieController.view];
Frame could also be black if you've set shouldAutoplay to YES and controlStyle to MPMovieControlStyleNone
--Edit
This is how I init my player and it works, maybe that will be of some help. It's playing HTTP Live Streaming video, but it'll play anything you put into it. You should try the url in this sample. If it'll work then there's definitely some problem with your contentUrl.
url = [NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayerController.view.frame = self.view.bounds;
[self.view insertSubview:moviePlayerController.view atIndex:0];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController prepareToPlay];
I had the same problem, and also following the same tutorial, but then I found the class moviePlayerViewController that its easier to use and you don't even have to worry about dismiss buttons.
I fixed used the following code: (I think it works better with ios5)
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:finalurl];
moviePlayerViewController.view.frame = self.view.bounds;
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
moviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer prepareToPlay];
moviePlayerViewController.moviePlayer.fullscreen=YES;
I am creating a application in which I can need to play a video which I do by mpmovieplayer controller .Now i nee to do this for both orientation .But the frame doesnt get set properly .
The code is as follws
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self shouldAutorotateToInterfaceOrientation:[UIDevice currentDevice].orientation];
NSURL *temp = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Floating" ofType:#"mp4"]];
mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:temp];
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
mpviewController.view.backgroundColor = [UIColor clearColor];
mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mpviewController.view.userInteractionEnabled= NO;
mpviewController.moviePlayer.fullscreen= YES;
mpviewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
[[mpviewController moviePlayer] play];
[self.view addSubview:mpviewController.view];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
currentOrientation = interfaceOrientation;
//[self SetInterface];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
mpviewController.view.frame = CGRectMake(0, 0, 1024, 768);
return YES;
}
I dont know where I am wrong.Please let me know for any chages to make in code. So as to get proper orientation.
Ragards Abhi
First
I believe you don't need to resize the mpviewController as it will resize it self alone.
you should only set the -
Second
In the shouldAutorotateToInterfaceOrientation you only set the supported directions in shouldAutorotateToInterfaceOrientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
If it dose not do it you change the view properties in -
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
//do what you want for portrait
}else{
//do what you want for landscape
}
}
you should only return either YES or NO in shouldAutorotateToInterfaceOrientation: method, it's called by framework only to get the information about the supported orientation in your controller, Read the apple documentation for the same.
you need to register for the orientation change notifictaion
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Implement your orientationChanged: method.
//********** ORIENTATION CHANGED **********
- (void) orientationChanged:(NSNotification *)note
{
NSLog(#"Orientation has changed: %d", [[note object] orientation]);
//Put your code here from shouldAutorotateToInterfaceOrientation:
}
Not forget it to remove .
[[NSNotificationCenter defaultCenter] removeObserver:self];
Here are some link
Device orientation - autorotate?
Orientation Changed Notification
No need to change any codings .. simple insert the following codings to the application , it will automatically detect the orientation...
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blackColor]];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"sharkdivertrailer" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184, 200, 400, 300);
[theMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
I want to show an MPMoviePlayer in landscape mode on launch of the application. Now it starts in portrait mode. There are some codes which forces application to launch in landscape mode. But it is said that these code segments belong to private api so app store will not accept the application. Since morning I am trying to find a way, but no result... Can anyone help me?
This is where I am:
NSURL *url = [[NSURL alloc] initWithString:urlString];
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];
//[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[moviePlayer play];
Thank in advance..
Open your project's info.plist under the Resource group in xCode.
select last line and click on the +(plus) icon. Now select the key 'Initial interface orientation' for value 'Landscape (left home button)'.
or you can select any value from the list shown on clicking drop down buttons.
If all your app is gonna be in landscape you can change it at your project's plist. If not, you can also subclass MPMoviePlayerControllerView and implement and do something like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation {
// Return YES for supported orientations.
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
return NO;
}
Use MPMoviePlayerViewController instead of MPMoviePlayerController. It will handle orientation, control style etc. I referred https://stackoverflow.com/a/4911172/3346048 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 return UIInterfaceOrientationMaskLandscape;
}
}
-(void)playVideo:(NSURL *)url{
if (videoPlayer) {
[videoPlayer release];
videoPlayer = nil;
}
videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
url = nil;
videoPlayer.moviePlayer.controlStyle=MPMovieControlStyleFullscreen;
videoPlayer.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
videoPlayer.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
videoPlayer.view.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication]
setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
[videoPlayer.moviePlayer prepareToPlay];
videoPlayer.moviePlayer.shouldAutoplay = YES;
videoPlayer.hidesBottomBarWhenPushed = YES;
self.navigationController.navigationBarHidden=NO;
videoPlayer.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:videoPlayer animated:YES];
[self addObservers];
}
Can anyone say what is wrong with this code? The url is streaming http link. Video player is appearing but after a while it disappears. It is working in 3G network but not in wifi
As you are saying that it was working on 3G but not on WiFi Network, your issue is most likely not related to the code but to the movie-file/s you are testing.
Make sure the m3u8-snippet is complete and valid also for low bandwidth. Test the m3u8 with Apples Mediastream Validator as described by this Best Practice Guide and this Article.
After this line:
videoPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
add this:
[videoPlayer retain];