MediaPlayerViewController rotates into view before play - iphone

My MediaPlayerViewController developed this funny thing of rotating into view! My whole app is landscape based, but the MediaPlayerViewController seems to rotate from portrait to landscape and when finished the previous view would then also rotate from portrait into its original landscape orientation. This will not happen in a small test app. Only when the app becomes complicated. I have tried preloading the movie, doesn't help, i have tried didRotatefromInterfaceOrientation, nothing. I have my supported orientations set and to return UIInterfaceOrientationIsLandscape. My code for the movie if that might help:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"BearA-least" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentModalViewController:moviePlayer animated:NO];
moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[moviePlayer.moviePlayer play];
I'm new to developing and have been struggling with this problem for a while. Any input would be greatly appreciated. I've seen people having similar problems with views rotating.

As I see, you're using MediaPlayerViewController and presenting it using presentModalViewController
MoviePlayerViewControllers are usually displayed using [self presentMoviePlayerViewControllerAnimated:(PlayerViewController)]
As I can see, you're preventing your user from controlling the playback -> MPMovieControlStyleNone
Hint: You might also want to set the movieSourceType property on your MoviePlayerController.
For controlling the orientation in a way as you're trying to achieve, I would suggest you to subclass MPMoviePlayerViewController and listen for notifications sent by the MPMoviePlayer on the various movie playback events.

Related

Getting a black screen when trying to play a movie

When i'm trying to play a movie on my iPhone, it isn't work. I dont know why.. searched here and all over the internet, try 5 different codes, but still can't get it to work. I'm new to Obj-C, so please be patient.
I just want the movie to play and when the movie is done, to exit the player. also, at the beginning I dont want it to show a black screen, I want to see the first frame of the video instead. is it possible?
-(void)playTheMovie
{
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"C061381" ofType:#"mp4" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
}
Thanks!
Most probably you are messing up with the URL.
Check out Movie player sample project from apple
https://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html
Run it first to confirm its functionality and then
Replace its movie with yours and change the extension and path name in the code
if it works copy the code to your project..

objective c MPMoviePlayerController wont work on iOS 4.3

Ive got this code from an ebook tutorial on embedding MPMoviePlayerController from a VIEW object but it just dont work at all on iOS 4.3, it just gives me a black screen. I've tried looking at other sources and they have the same source code. Can anyone help me on finding what is the problem in this code.
Thanks
- (IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"shawarma" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[player.view setFrame: movieHolder.bounds];
[movieHolder addSubview: player.view];
[player play];
}
My VIEW object has a dimension of 400 x 300.
From the MPMoviePlayerController class reference guidelines Consider
a movie player view to be an opaque structure.
You can add your own custom subviews to layer content on top of the
movie but you must never modify any of its existing subviews.
In addition to layering content on top of a movie, you can provide
custom background content by adding
subviews to the view in the backgroundView
MPMoviePlayerController it self has a property view to present the video
Hope this LINK might help you
There is no issue with MPMoviePlayer and ios 4.3 i am working on an application that plays movie from a server and this is working fine for me. I request you to check
Path of you resource try some hardcode path.
Frame of the MoviePlayer.
Thanks

Adding video to an iPhone application

How do I add a video to an iPhone app. using iPhone SDK 3.2.5?
I dont see the actual video but I can hear it playing.
Here is the code:
-(void) viewDidLoad{
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Facebook" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
[super viewDidLoad];
}
First off – find out what video format you are having. I assume it's not the one supported by iPhones.
So when you learn this you can go ahead and look for a third party player that is able to play back your video. I'm sure there are a lot of them there. Recommendation from me – try using VLC.
If you want to use default videos app on your iPhone (which, of course, is way better than any other alternative) you can try out Windows & Mac application WALTR 2: https://softorino.com/waltr/windows-guides/how-transfer-videos-to-iphone-without-itunes. It adapts any video & music for iPhone playback and pushes them to your default app on an iPhone.
You need to set the frame for you MPMoviePlayerController and add it to your view:
theMovie.view.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
[self.view addSubview:theMovie.view];

Play videos fullscreen in landscape (iPhone)

I have an app that is built as a hierarchy of viewControllers.
On of the view Controllers is for a 'video section' of the app.
The app is designed to be portrait only, however, I want to force the videos to play fullscreen in landscape (just like the iPod app on the iPhone).
After searching around, I see that many people have this problem.
I finally have been able to rotate it, but it doesn't work in full screen, that defaults to portrait.
And since it doesn't work in full screen, you see elements in the parent views over the video.
Is there an easy way to rotate this video in full screen, or do I have to broadcast a message to the parent views to hide elements when the video is playing?
here is the code:
NSURL *url = [NSURL URLWithString:[recipeData objectForKey:#"videoPath"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.shouldAutoplay = YES;
moviePlayer.view.frame = [[UIScreen mainScreen] applicationFrame];
moviePlayer.view.transform = CGAffineTransformMakeRotation(1.57079633);
moviePlayer.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
[self.view addSubview:moviePlayer.view];
//commenting out the line below will rotate the video, leaving it uncommented forces it to play fullscreen in portrait
[moviePlayer setFullscreen:YES animated:NO];
I put the method to create the video in the appDelegate and target it whenever the video is launched. This did the trick, keeping it above everything else.
Have you tried using MPMoviePlayerViewController instead? it presents a full screen almost modal view Controller to handle the video playing. I use it in >=3.2 version and use a hacked up version of MPMoviePlayerController for <3.2
For full screen playback you should use MPMoviePlayerViewController and then to make it launch and play in landscape format use the "shouldAutorotateToInterfaceOrientation" method on the MPMoviePlayerViewController class.
Looks like this:
[yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];

MPMoviePlayerController questions, best practices

I have any number of thumbnail images that, when tapped, will play a different video (fullscreen). I have never been clear on whether I should keep one MPMoviePlayerController object in my view controller and have it play whichever url according to the thumbnail that was tapped, or create a new MPMoviePlayerController each time. What is the best practice?
I am also having problems where tapping on different thumbs crashes the app, I believe because the MPMoviePlayerController tries to stream a video while it is already trying to stream. There seems to be no way to cancel a MPMoviePlayerController and clear out what it was doing, then start loading a new video.
Here's how I create it:
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] init];
self.player = moviePlayer;
[moviePlayer release];
Then to play a video I do this:
//would like to do something like this first - [self.player clear];
self.player.contentURL = someURL;
[self.view addSubview:player.view];
[self.player prepareToPlay];
[self.player play];
Any advice is welcome... thanks.
When you are changing the video in an MPMovieplayerController,then you can remove the mpmoviecontrollerplayer view from super view using removeFromSuperView and again add it's subview to the super view initializing it with new URL.
No need to create new object every time.