I'm using the following code to play a movie on an iPad 1 running iOS6:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myMovie.mov" ofType:#""]];
self.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:self.moviePlayer];
// Use the new 3.2 style API
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
[self.moviePlayer prepareToPlay];
self.moviePlayer.view.frame = CGRectMake(0, 0, 1024, 768);
[self.uiView addSubview:self.moviePlayer.view];
//[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
This works.
If however I uncomment the line for setFullscreen, I get the movie audio but with a completely black screen, no picture.
I've tried changing the order of a few lines, in particular the play call and the setFullscreen call, with no effect.
Update
This question appears to be directly relevant:
MPMoviePlayerController in full screen issue (showing blank black screen)
first of all change this line : [[NSBundle mainBundle] pathForResource:#"myMovie.mov" ofType:#""] to: [[NSBundle mainBundle] pathForResource:#"myMovie" ofType:#"mov"] and try again
Use this as its working for me:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myMovie" ofType:#"mov"]];
MPMoviePlayerViewController *movieView = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
movieView.moviePlayer.scalingMode = MPMovieScalingModeFill;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[movieView.moviePlayer.view setFrame:CGRectMake(0,0,480,320)];
else
[movieView.moviePlayer.view setFrame:CGRectMake(0,0,1024,768)];
movieView.moviePlayer.view.userInteractionEnabled = FALSE;
movieView.moviePlayer.controlStyle=0;
[movieView setFullscreen:YES];
self.view=movieView.view;
Related
This code is working perfect in iOS 8 . but unable to play video in iOS7.The player shows up but plays nothing.Thanks
NSString *url = #"http://.../playlist.m3u8";
NSURL *streamURL = [NSURL URLWithString:url];
self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
self.streamPlayer.controlStyle= MPMovieControlStyleFullscreen;
self.streamPlayer.movieSourceType = MPMovieSourceTypeStreaming;
self.streamPlayer.shouldAutoplay = YES;
[self.view addSubview:self.streamPlayer.view];
[self.streamPlayer prepareToPlay];
[self.streamPlayer play];
From the documentation:
When you add a movie player’s view to your app’s view hierarchy, be sure to size the frame correctly, as shown here:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
Try to set the frame of your video player.
I don't want to play a video in fullscreen, is it possible to play a video inside a view?
Here is my code. It does create a frame and plays the video inside it. The problem is that I don't know how to center on screen the frame (it appears on the right side of the screen)
Screenshot
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[bundle pathForResource:#"movie" ofType:#"mov"]];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
_moviePlayer.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:_moviePlayer.view];
_moviePlayer.shouldAutoplay = YES;
replace a line with given below will fix your issue.
_moviePlayer.view.center = self.view.center;
I wanted to play a video in ios 6. I am using Xcode 4.5. I write code below but getting error
Test Demo[736:14003] [MPAVController] Autoplay: Disabling autoplay for pause
Test Demo[736:14003] [MPAVController] Autoplay: Disabling autoplay
Test Demo[736:14003] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
and my code
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"TestVideo" ofType:#"m4v"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];`
There could be many issues for your error.
1) Set a property for your MPMoviePlayerController and synthesize it. This may occur, if your MPMoviePlayerController releases before it plays.
2) add this code player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
3) As per documentation, .m4v extension is not mentioned to play.
4) Below screenshot will help you about supported formats.
NSString *url = [[NSBundle mainBundle] pathForResource:#"santaMessage" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
[moviePlayer play];
///this worked like a charm
In my iPad app, I have a UIButton that is calling an IBAction to call a view controller as a modal to show a video in. I wanted the modal to appear as 720x405, and that part seems to work out okay. Here is the IBAction code the button is executing:
-(IBAction)videoPlayerTest:(id)sender {
VideoModalViewController *vc = [[VideoModalViewController alloc] initWithNibName: #"VideoModalViewController" bundle: nil];
vc.fileName = #"testvideo.m4v";
vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:vc animated: YES];
vc.view.superview.frame = CGRectMake(0, 0, 720, 405);
vc.view.superview.center = self.view.center;
[vc release];
}
The modal comes up where I want it, and the controls respond on the MPMoviePlayerController; the jog bar, pause, play, etc. but if the user taps on the fullscreen button, the video does go fullscreen alright, but after that the MPMoviePlayerController won't respond to any subsequent taps on the player controls. If I remove the modalPresentationStyle line it will work, but the modal appears on a fullscreen view instead of the 720x405 modal like I want. I've added Observers to try resizing the frame and recenter it when the user makes the movie controller fullscreen and back to windowed, but it didn't appear to help at all. Here is that code.
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
[self setModalPresentationStyle:UIModalPresentationFullScreen];
self.view.frame = CGRectMake(0, 0, 1024, 768);
self.view.center = self.view.center;
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
[self setModalPresentationStyle:UIModalPresentationFormSheet];
self.view.frame = CGRectMake(0, 0, 720, 405);
self.view.center = self.view.center;
}
- (void)playMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSURL *videoURL = [NSURL fileURLWithPath:videoString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 720, 405);
moviePlayer.view.backgroundColor = [UIColor grayColor];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
This is my first post-- hope I did it right and provided enough information about the problem I'm having.
I've solved my problem. I was unaware of MPMoviePlayerViewController and I created that and used that as my modal instead. It works great.
-(void)playVideo:(NSString *)fileName {
NSString *videoString = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSURL *videoURL = [NSURL fileURLWithPath:videoString];
mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentModalViewController:mpViewController animated:NO];
[[mpViewController moviePlayer] play];
}
-(IBAction)videoPlayerTest:(id)sender {
[self playVideo:#"testvideo.m4v"];
}
Thought I'd post what I came up with just in case somebody else encounters the same
I am working on MPMoviePlayerController, I am playing a video which is in URL #"http://www... xyz... ITs not playing in simulator.
I am
NSString *url = #"hhttp://www... xyz... ";
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:url]];
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(50, 50, 200, 200);
[moviePlayer play];