Movie starts playing automatically? - iphone

I have added a movie to my view and it simply starts playing automatically, does anyone know how to stop this?
MPMoviePlayerController *moviePlayer;
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"LAN_S9" ofType:#"mp4"];
if (moviePath)
{
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[moviePlayer view] setFrame: CGRectMake(0, 0, 400, 200)]; // frame must match parent view
}
[self.view addSubview: [moviePlayer view]];

Thats simple:
moviePlayer.shouldAutoplay = NO;

You cannot add a view of MPMoviePlayerController into your view because the MPMoviePlayerController always require full screen mode to play and it will auto jump into that mode.
So, you can extract an image from the film and then show the image with a nice Play button so user can tap on the button to view the video

Related

How to quit MPMoviePlayerController programmatically?

I need to quit the MPMoviePlayerController programmatically while playing, instead of pressing done button. Is it possible. Is there any way to simulate Done button click?
i have one trick for you. you can take the mpmovieplayer on uiview and then remove the uiview after stoping the player
In ViewController.h
MPMoviePlayerController *moviePlayerController;
UIView *view1;
-(IBAction)cancelPlay:(id)sender;
In ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"try" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
view1=[[UIView alloc]initWithFrame:CGRectMake(0, 10, 320, 300)];
view1.backgroundColor=[UIColor blueColor];
[self.view addSubview:view1];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 10, 320,300)];
[view1 addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[moviePlayerController play];
}
-(IBAction)cancelPlay:(id)sender{
[moviePlayerController stop];
[view1 removeFromSuperview];
}

iPhone:MPMoviePlayer shows black background for fraction of second initially

In my application I play video on one of the view which contains following code
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"bgVideo" ofType:#"mov"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setControlStyle:MPMovieControlStyleNone];
player.view.frame = CGRectMake(35, 190, 245, 156);
[self.view addSubview:player.view];
[player play];
[player.view setBackgroundColor:[UIColor clearColor]];
I wrote this code on viewWillAppear method
but when I come to this view initially my MPMoviePlayerController shows black screen before starting the video for fraction of second.
I don't want black screen for second.
what shoud I do for it?
thanx in advance.
maybe player a strong property and synthesize it. It worked.
should work in your case as well.
My experience, will not problem any more. Please reply if you can not work.
Edit
I had a little mistaken.
Black screen is shown before the start, because the video is not loaded.
you can not predict the time taken to loading the video. Even if you allocate player to viewWillApper method. black screen will appear.
This black screen can not control it. YouTube app, or a default video App also same.
In During a Black screen, ActivityIndicator or "Loading" message should show to user. It is reasonable.
Finally In general, the larger the video size and quality, takes longer to load.
if you want exactly loading time, you should be notified.
refer a sample code.
- (void)viewWillAppear:(BOOL)animated
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"]];
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setControlStyle:MPMovieControlStyleNone];
player.view.frame = CGRectMake(35, 190, 245, 156);
[self.view addSubview:player.view];
[player.view setBackgroundColor:[UIColor clearColor]];
player.shouldAutoplay = NO;
[player prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loadMoviePlayerStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:self.player];
[super viewWillAppear:animated];
}
- (void)loadMoviePlayerStateChanged:(NSNotification *)noti
{
int loadState = self.player.loadState;
if(loadState & MPMovieLoadStateUnknown)
{
NSLog(#"MPMovieLoadStateUnknown");
return;
}
else if(loadState & MPMovieLoadStatePlayable)
{
NSLog(#"MPMovieLoadStatePlayable");
[player play];
}
else if(loadState & MPMovieLoadStatePlaythroughOK)
{
NSLog(#"MPMovieLoadStatePlaythroughOK");
} else if(loadState & MPMovieLoadStateStalled)
{
NSLog(#"MPMovieLoadStateStalled");
}
}

iPhone app: play video in small view only

In my iPhone App I want to play video in a small view.
Video should start playing automatically, and the buttons for play pause and full screen should be hidden.
How can I achieve that?
What you would do is manually set the frame for MPMoviePlayerController's view, and set the control style to MPMovieControlStyleNone. Much like this below..
....
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[player setControlStyle:MPMovieControlStyleNone];
//this is where you create your `embedded` view size
player.view.frame = CGRectMake(0, 0, 200, 300);
[self.view addSubview:player.view];
....
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
[player prepareToPlay];
player.shouldAutoplay = NO;
player.allowsAirPlay = YES;
player.scalingMode = MPMovieScalingModeAspectFit;
[player.view setFrame:CGRectMake(0,0,300,320)];
[viewPlayer addSubview:player.view];
self.moviePlayer = player;//declare MPMoviePlayerController class object globaly for play and push the vedio if you want to add this functionality
hope,this help you....
:)

MediaPlayer Framework iOS SDK

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;

add running videos in a scrollView

I have created a scrollView and need to add some running videos to it.
I did the following thing:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
NSURL *fileUrl1 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AUDI_anti_crevaison_1" ofType:#"mp4"]];
NSURL *fileUrl2 = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:#"AUDI_Utilisation_de_la_roue_de_secours_2" ofType:#"mp4"]];
videoPlayer1 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl1];
videoPlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:fileUrl2];
[self.scrollView addSubview:videoPlayer1.view];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width *2, self.view.frame.size.height);
[self.view addSubview:scrollView];
[scrollView release];
First of all, it seems this line: [self.scrollView addSubview:videoPlayer1] is broken, I cannot add MPMoviePlayer to a UIView.
And second how can I set the frame for each video...the xOrigin and yOrigin?
I would appreciate a complete code with adding .mp4 files to a UIScrollView cause I cannot seem to make it work.
Thank you!:)
You should read the documentation first.
You're trying to add a controller instead of a view that's why it
doesn't work. I see that you have Android experience - it's like the difference between activity and view on android
If you still can't figure it out there's a lot of code for adding videos to views on the web, just search it. Don't see it necessary to add here such a generic code.