I use MPMoviePlayerController to play a local file in my Application Document folder which have I have downloaded for a server URL:
itemMoviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:itemMoviePlayerController.view];
itemMoviePlayerController.fullscreen = YES;
itemMoviePlayerController.movieSourceType = MPMovieSourceTypeFile;
itemMoviePlayerController.initialPlaybackTime = -1.0;
[itemMoviePlayerController play];
When I play .mov file just after I downloaded it, It shows up a black empty screen & app UI is unusable.
But if play same local file next time, it plays fine.
I even verified playState & localState for MPMoviePlayerController they seems fine.
What could be reason for black empty screen?
You need to retain your instance of MPMoviePlayerController i.e. as a property or an instance variable. The reference to the movie player is lost if you do not retain it.
You could try to put [itemMoviePlayerController prepareToPlay];
before the [itemMoviePlayerController play];
The way preferred by Apple to display an only full screen video is to present a modal MPMoviePlayerViewController (as Hollance said).
To present it, you should use something like :
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
[itemMoviePlayerController play];
This is documented by Apple here
You can read there that you should keep a reference to your MPMoviePlayerViewController in order to dismiss it later with
[self dismissMoviePlayerViewControllerAnimated:moviePlayerViewController].
I fixed this by putting
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
in my .h file, and calling self.moviePlayer in all my code and it worked!
You need to use MPMoviePlayerViewController instead. Notice the word "View" in there.
I hope this will help. I solved this problem in my project
-(void)startPlayingMovie
{
NSLog(#"startPlayingMovie");
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Start_video" ofType:#"mov"]];
moviePlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer.moviePlayer];
moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.moviePlayer.shouldAutoplay = YES;
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer setFullscreen:YES animated:NO];
NSLog(#"endPlayingMovie");
}
Same thing happened to me. It turns out, I had tried to play the movie while the UIImagePicker wasn't dismissed yet.
In my UIImagePickerDelegate I had to first dismiss the UIImagePicker Popup and then open the ViewController managing the MPMediaPlayerController:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)infoDict
{
//the first thing to do: dismiss the media picker
if ([ipPop isPopoverVisible])
{
[ipPop dismissPopoverAnimated:ANIMATION_SETTING];
}
myMediaPlayerViewController * playerVC = [[myMediaPlayerViewController alloc] initWithMediaDict:infoDict];
[self.navigationController pushViewController:playerVC animated:ANIMATION_SETTING];
}
I had this same problem and it made me crazy. It would work on one view controller fine (audio and video), but not work on another (black screen with just audio). In the end, all I did was CHANGE THE ORDER of the calls: I simply waited until I was done configuring the movie player before adding it to the view. In other words, I called "addSubview" on the line just before the call to "play".
This is the code I'm using to play a file from URL:
MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:contentUrl]];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController release];
It seems to work fine for me. Two notes:
Some simulators (like the current iOS 5.0) crash when playing a movie, but it works on a real device
If you leave out the movieSourceType part, a black screen is shown for about a second before the movie starts
I had the same problem, solved it changing the extension of the file being played from .mpg to .mp4. apparently MPMoviePlayerController expects a correct extension, though from the documentation it is not clear to me that this is a requirement:
http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html
Supported Formats This class plays any movie or audio file supported
in iOS. This includes both streamed content and fixed-length files.
For movie files, this typically means files with the extensions .mov,
.mp4, .mpv, and .3gp and using one of the following compression
standards:
This issue seems fixed after updating device iOS to 5.0.
Seems to iOS SDK issue for previous version
you must do like that with CGRectMake(0, 0, 0, 0) in the finished callback!
-(void)playMovieAtURL:(NSURL*)theURL
{
MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
theMovie.scalingMode=MPMovieScalingModeAspectFill;
theMovie.view.frame = CGRectMake(2, 246, 317, 70);
[self.view addSubview:theMovie.view];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
theMovie.view.frame = CGRectMake(0, 0, 0, 0);
// Release the movie instance created in playMovieAtURL
[theMovie release];
}
I was having a similar issue. After playback there comes an empty screen, which prevents me to play video again.
Well here is my work around. That way play button doesn't disappear.
[self.movieController stop];
[self.movieController prepareToPlay];
[self.movieController pause];
Also, If you are using http URLs, make sure you disable App Transport Security in your projects *.plist file. It will also cause a black screen.
Add the following code:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
Here:
[INSERT CODE ABOVE HERE]
</dict>
</plist>
----- BOTTOM OF PLIST FILE
Related
I want to play mp4 video files using MPMoviePlayerController.
Here is the code I am currently using:
NSURL *url = [NSURL URLWithString:
videoLink];
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc ]init];
[controller`enter code here` prepareToPlay];
self.mp = controller; //Save obj reference
Also I am re using same player object because have to load another video file as soon as next or previous clicked on UI.
controller.view.frame = CGRectMake(0, yMargin, self.view.frame.size.width, self.view.frame.size.width*9/16); //Set the size
[self.view addSubview:controller.view];//Show the view
[controller setContentURL:url];
[controller play]; //Start playing
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:self.mp ];
Sometimes it's loading video properly but randomly showing an error message as given below.
itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
If error comes it doesn't load any other video afterwards at least for next 15(approx.) attempts.This behaviour is very random as sometimes it keeps showing the error in log and player doesn't load the video at all.
Has someone else faced this similar issue ? I found many questions posted related to this problem but nothing seems to be working for me.
Other solution i found playing a video using web view but auto play
doesn't work for web view.
I have a MPMoviePlayerViewController, I don't want to initialize it with a content url, I just want to have a empty movie player GUI out there. Whenever I want it to load a movie, it then starts loading. But I can't
self.playerViewController = [[MPMoviePlayerViewController alloc] init];
MPMoviePlayerController *player = [self.playerViewController moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
player.shouldAutoplay = FALSE;
player.initialPlaybackTime = 0;
[player setFullscreen:FALSE];
[player.view setFrame:CGRectMake(0, -20, self.view.bounds.size.width, self.view.bounds.size.height/2)];
[self.view addSubview:player.view];
As you see, I init the MPMoviePlayerViewController with no content url, and I set the autoplay to false, but when I run the application. Even though there is no video url given, I can still see a label "Loading ..." near the "Done" button, why???? How can I remove the loading label?
Just use MPMoviePlayerController instead of MPMoviePlayerViewController. You'll need to build a view controller around it if you want to display it modally.
You could also maybe iterate through [MPMoviePlayerViewController view].subviews until you find the UILabel, but that approach might break in future versions of iOS.
This is probably because I am having a brain freeze but I don't believe I have come across MPVideoView before.
So I have a UIWebview with a HTML5 demo in it, this then plays a video (in the web view) which seems to create a new view of type MPVideoView. IS this a private class for Apple to use to show HTML5 Video content?
Does it have any delegate methods? So I could auto play it?
Can I move the controls about, I am guessing they are a subview of that view?
You can use MPMoviePlayerController for playing HTML5 video(considering you have the link to the video)
Use the following code
moviePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
moviePlayer1.view.frame = CGRectMake(0, 0, 200, 110);
[self.view addSubview:moviePlayer1.view];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer1];
[moviePlayer1 play];
Delegate methods can be found here
I have a weird problem for you all.
MPMoviePlayerController is playing the video fine, and the audio ONLY plays through headphones.
The real drag is that this only happens on some iPads and iPhones, even the SAME EXACT MODELS
running the SAME EXACT SYSTEM!
I've created a simple failing example here:
http://www.porcaro.org/MPMoviePlayerController/TestMovie.zip
I've seen it run fine and fail on iPhone 4S, iPhone 4 and iPad 2.
Here is the most relevant code. Thanks for any insight, I'm going to submit a bug to Apple as well:
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
moviePath = [NSString stringWithFormat:#"%#/intro.m4v", [[NSBundle mainBundle] bundlePath]];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
theMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
controlStyle = MPMovieControlStyleEmbedded;
movieView = [self view];
movieRect = [[self view] frame];
controlStyle = MPMovieControlStyleFullscreen;
theMoviePlayer.controlStyle = controlStyle;
theMoviePlayer.view.userInteractionEnabled = YES;
if (1) {
NSLog(#"Created theMoviePlayer: %#. Playing: %#", theMoviePlayer, moviePath);
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(checkForEndOfMovie:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:theMoviePlayer];
// this line doesn't fix the problem
//[theMoviePlayer prepareToPlay];
[[theMoviePlayer view] setFrame:movieRect];
[movieView addSubview: [theMoviePlayer view]];
[theMoviePlayer play];
}
This is an old question but maybe it'll help someone. I came across the same issue and found out it was happening when the phone is in silent mode only.
Solution is to set the player's useApplicationAudioSession property to false.
[theMoviePlayer setUseApplicationAudioSession:NO];
This problem was happen to me once playing video on iPad 2 using mpMoviePlayer. The video was playing perfectly but audio only comes when earphones connected.
Solution to solve the problem:
[theMoviePlayer setUseApplicationAudioSession:NO];
I am having the toughest time playing a video after select anything from a UIImagePickerController when the UIImagePickerController is presented in a UIPopoverController.
Here is the code that presents the video:
-(void) presentMovie{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mov"];
NSURL *movieURL = [NSURL URLWithString:filePath];
[[mp moviePlayer] setContentURL:movieURL];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setShouldAutoplay:NO];
[[mp moviePlayer] setControlStyle:MPMovieControlStyleEmbedded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoPlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[mp moviePlayer] setAllowsAirPlay:YES];
[[mp view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[screen addSubview:mp.view];
[[mp view] setFrame:screen.bounds];}
mp is a MPMoviePlayerViewController ivar that is allocated and initialized in my viewDidLoad
[self presentMovie] works perfectly unless it is being called from within the -imagePickerController:didFinishPickingMediaWithInfo: method
When called from that delegate methode, the video being displayed by MPMoviePlayerVideoController simply disappears from as soon as it is played. If the 'shouldAutoPlay' property is set to YES, I just see loading for a split second and then blackness. If the 'shouldAutoPlay' property is set to NO, then I see the first frame of the video, and I can scrub to a different location in the vido, take the video in and out of full screen, etc. but soon as I hit play, the view that the video is in goes black. This is true if the video that I'm attempting to play is from the UIImagePickerController selection or from the mainBundle. Once the UIImagePicker is selected, the video will not play.
I have gone through several steps of debugging and this does not happen with the UIImagePickerControll is presented in a modal view on the iPhone only when it's in a UIPopoverController on the iPad.
any ideas? anyone else able to successfully play a video on the iPad selected from a UIImagePickerController?
Turns out the answer is "roll your own" using ALAsssetsLibrary. UIImagePickerContorller doesn't work with video on iPads, and creating your own custom picker isn't too hard. There's sample code from the WWDC 2010 as well as code from Mat Tuzzolo on GitHub https://github.com/elc/ELCImagePickerController
I had this problem, that the video would not play after being picked from UIImagePickerController, but I wasn't fully releasing the popover. When I released it properly, it worked fine.
Thanks to prudence # UIImagePickerController choosing video prevents MPMoviePlayerViewController instance from working