MPMoviePlayerController dismisses my UIImagePickerController - iphone

I have an iPhone app where I load an UIImagePickerController onto a UIViewController. I then have a custom view on top of the camera. Now when the user takes a photo it is loaded onto a UIImageView which presents it to the user asking if you want to use that photo or take another (removing the image from the UIImageView). This works perfectly.
Now If the user has just recorded a video I wanted to take a snapshot preview somewhere in the video and present it as a static image in the same UIImageView. I do this with the following code:
MPMoviePlayerController *videoPlayer = [[MPMoviePlayerController alloc] init];
videoPlayer.shouldAutoplay = NO;
[videoPlayer setContentURL:[info valueForKey:UIImagePickerControllerMediaURL]];
UIImage *videoScreenShot = [videoPlayer thumbnailImageAtTime:(videoPlayer.duration/2.0) timeOption:MPMovieTimeOptionNearestKeyFrame];
photoPreview.image = videoScreenShot;
[videoPlayer release]
This works as intended. The problem is if I want to take another video. When I call:
[videoPlayer setContentURL:[info valueForKey:UIImagePickerControllerMediaURL]];
The camera shutter closes and the camera is seemingly dismissed. Trying to take a picture or recording video gives me:
UIImagePickerController: ignoring request to take picture; camera is not yet ready.
UIImagePickerController: ignoring request to start video capture; camera is not yet ready.
I've tried calling the following after, which has no effect:
myImagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
[myImagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
I tried adding the UIImagePickerController to the UIIViewController again, which caused some fantastic freeze-ups.
Finally I tried commenting out the UIViewController's [super didReceivedMemoryWarning] line of didReceivedMemoryWarning. Which also had no effect.
I'm guessing MPMoviePlayerController takes over something UIImagePicker also needs. How do I give it back?

I would suggest setting your MPMoviePlayerController to nil before showing the UIImagePickerController, or maybe taking its view out of the interface temporarily. Both of these contain a movie player view, but there can be only one movie player view at a time in your application's interface. Thus they can interfere with each other, and that might be what's happening to you.

Related

Playing videos on iPad with a specific style

i have some mp4 files which are to be played in my iPad app.. I am able to do that quite well. right now i have a play button on blank black space in my app and the video plays after i tap the play button. The user will only come to know the content of the video after playing it.. But, i want the user to know about the video before playing itself . instead of the default black screen i want to show the video starting screen to make the video more interesting. To put it in simple words, i want my video space to be similar to youtube... IS there any way i which this can be done?? Thanks
Subclass MPMoviePlayerController (or however you're playing your video.. if you already use a custom class just add the code in there) and in viewDidAppear initialise a UIImageView with frame size equal to self.view.bounds using whatever background image you want for the loading screen. Add the UIImageView as a subview of self.view and call sendSubviewToBack: on it. When the player is ready to play, it will start drawing video frames on top of your subview, and you should not see it again.
- (void)viewDidAppear
{
UIImageView *loadingImage = [[UIImageView alloc] initWithImage:myImage];
[loadingImage setFrame:self.view.bounds];
[self.view addSubview:loadingImage];
[self.view sendSubviewToBack:loadingImage];
[super viewDidAppear];
}

UIImagePickerControllerSourceTypeCamera crashes in IPHONE Simulator?

Is it a known issue that if you try to test your UIImagePickerController using the Camera as a source type then the simulator will crash?
I have the following code:
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
[self.imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentModalViewController:self.imgPicker animated:YES];
[imgPicker release];
Running this in simulator, I get an objc-exception-throw error on the stack # -[UIImagePickerController setSourceType:].
Now if I set the source type to the Photo Library though, everything runs smoothly and fine? What's the deal?
Simulator doesn't have the camera and can't simulate to take a picture (it would have been nice to use the isight but Apple has not been so kindly). However Your code is not safe because, for example, old ipod touch doesn't have a camera and in this case your app will crash on this device.
As Apple suggest in UIImagePickerController documentation:
To use an image picker controller
containing its default controls,
perform these steps:
1.Verify that the device is capable of picking content from the
desired source. Do this calling the
isSourceTypeAvailable: class method,
providing a constant from the
“UIImagePickerControllerSourceType”
enum.
2.Check which media types are available, for the source type you’re
using, by calling the
availableMediaTypesForSourceType:
class method. This lets you
distinguish between a camera that can
be used for video recording and one
that can be used only for still
images.
3.Tell the image picker controller to adjust the UI according to the
media types you want to make
available—still images, movies, or
both—by setting the mediaTypes
property.
4.Present the user interface by calling the
presentModalViewController:animated:
method of the currently active view
controller, passing your configured
image picker controller as the new
view controller.
5.When the user taps a button to pick a newly-captured or saved image
or movie, or cancels the operation,
dismiss the image picker using your
delegate object. For newly-captured
media, your delegate can then save it
to the Camera Roll on the device. For
previously-saved media, your delegate
can then use the image data according
to the purpose of your app.
So you have to call isSourceTypeAvailable and set your sourceType consistently.

Stop iPad MoviePlayer video when a view is changed

I'm writing my first iPad app that plays a video on a portion of the screen. My problem is that if the user changes to another view while the video is playing, the audio keeps playing in the background. I assume I have to add something to the "viewDidUnload" method but I'm not sure what to do. Any ideas? Thanks for any info.
You can try adding
[myMoviePlayer pause]; // assume myMoviePlayer is an instance variable
[myMoviePlayer stop];
myMoviePlayer = nil;
to your viewWillDisappear method.

MPMoviePlayerController for iPhone App on iPad

For some reason, the expand button the arrow points to in the screenshot below causes the view controller that initiated video playback to animate back over top of the video, but without stopping video playback which means you can still hear the audio even though the video is no longer visible. I've tried other movie control styles, but there are other problems with those (for example, no controls causes the player to play the entire video before dismissing, i.e. no 'Done' button).
Here is the code that initiates the video playback:
player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[player setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[[self navigationController] presentModalViewController:player animated:YES];
[[player moviePlayer] play];
Any ideas/suggestions as to how I can either disable that button or receive its notification so I can respond accordingly?
Thanks.
I can't find a solution to this, but I did find a workaround. I simply call [player stop]; in my -viewDidLoad of the calling view controller. The outcome won't be what the user expects when they press that button, but it's better than allowing the video to continue to play when they press it.

Issue..with video playback on iPad with MPMoviePlayerViewController

I have been fighting some code for about a week, and am hoping that someone else may have experienced this problem and can point me in the right direction.
I am using the MPMoviePlayerViewController to play a video on the iPad. The primary problem is that it works FLAWLESSLY on the iPad Simulator, but will not play at all on the iPad. I have tried re-encoding the video to make sure that isn't an issue. The video I'm using is currently a 480x360 video encoded with H.264 Basline 3.0 with AAC/LC audio. The video plays fine on the iPhone, and also does play through Safari on the iPad. The video actually loads, and you can scrub through the video with the scrubber bar and see that it is there. The frames actually display, but just will not play. If you click play, it just immediately stops. Even when I have mp.moviePlayer.shouldAutoplay=YES set, you can see the player attempt to play, but only for a split second (maybe 1 frame?).
I have tried just adding view with the following code:
in .h
------
MPMoviePlayerViewController *vidViewController;
#property (readwrite, retain) MPMoviePlayerViewController *vidViewController;
in .m
------
MPMoviePlayerViewController *mp=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoURL]];
[mp shouldAutorotateToInterfaceOrientation:YES];
mp.moviePlayer.scalingMode=MPMovieScalingModeAspectFit;
mp.moviePlayer.shouldAutoplay=YES;
mp.moviePlayer.controlStyle=MPMovieControlStyleFullscreen;
[videoURL release];
self.vidViewController = mp;
[mp release];
[self.view addSubview:vidViewController.view];
float w = self.view.frame.size.width;
float h = w * 0.75;
self.vidViewController.view.frame = CGRectMake(0, 0, w, h);
I have also just tried to do a:
[self presentMoviePlayerViewControllerAnimated:self.vidViewController];
which I actually can not get to orient properly...always shows up in Portrait and almost completely off the screen on the bottom, and the app is only intended to run in either of the Landscape views...
If anybody needs more info, just let me know. I'm about at my wits end on this. ANY help will be GREATLY appreciated.
I see 2 problems here.
First you are not setting the frame of the view. Try adding
mp.view.frame = self.view.frame;
This is what fixed it for me.
Next the shouldAutorotateToInterfaceOrientation does not need to be called. This method simply tells you if the view supports an orientation. Check the Apple docs.
You may want to try shutting down the iPad (powering off), remove and re-install the app.
Just add a line after you add the movie player to the view:
[self.vidViewController.moviePlayer play];
See if it helps.
Thanks,
Madhup