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");
}
}
Related
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....
:)
I have an app where I play a movie after a touch on the view and then when the movie is finished. I place a button on the view. On clicking the button a second movie plays. But it seems that it is only then loaded and a short black screen appears. I want to avoid the black screen now...
Heres the code:
Initialization in viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *movpath = [[NSBundle mainBundle]
pathForResource:#"movie1"
ofType:#"m4v"];
mpviewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mp = [mpviewController moviePlayer];
// [mp setMovieControlMode:MPMovieControlModeHidden];
// [mp.view setFrame:CGRectMake(0, 0, 250, 263)];
mp.controlStyle = MPMovieControlStyleNone;
mp.shouldAutoplay = NO;
[mp prepareToPlay];
[mp pause];
then on a storyboard touch I call startAnimation
- (IBAction)startAnimation:(id)sender {
NSLog(#"Animation 1");
[self.view addSubview:mpviewController.view];
[mp play];
}
after this movie finishes I set the button
- (void) movieFinishedCallback:(NSNotification*) aNotification {
NSLog(#"...movie done");
// generate start button for animation 2
startAnimation2Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startAnimation2Button.tag = 1;
[startAnimation2Button addTarget:self action:#selector(startAnimation2:) forControlEvents:UIControlEventTouchUpInside];
startAnimation2Button.frame = CGRectMake(130, 230, 070, 070);
startAnimation2Button.userInteractionEnabled = YES;
startAnimation2Button.alpha = 0.1;
[self.view addSubview:startAnimation2Button];
}
then after a touch on the button, the second animation starts
- (IBAction)startAnimation2:(id)sender {
NSLog(#"Animation 2");
NSString *movpath = [[NSBundle mainBundle]
pathForResource:#"movie2"
ofType:#"m4v"];
[mp setContentURL:[NSURL fileURLWithPath:movpath]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback2:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[mp play];
}
but here a short black screen appears, probably while movie2 is loaded and is then playing.
How can I avoid the black screen?
Greetz
You will not be able to entirely get rid of the pre buffering delay (black phase) between the videos when using MPMoviePlayerController.
Use AVQueuePlayer instead.
AVQueuePlayer is a subclass of AVPlayer you use to play a number of items in sequence.
See other questions and issues on that matter.
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;
So I have an alert view pop up in my application and on clicking a view button a video is supposed to play but nothing happens in the simulator. I don't want to test this on a device until I get it working in the simulator. This is the code below as far as I can tell it should work. It reaches the Log statement and outputs to the console but the video does not play. Any ideas?
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex{
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:#"http://srowley.co.uk/endyVid.mp4"];
MPMoviePlayerController *player = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
//---play movie---
[player setFullscreen:YES];
[self.view addSubview:[player view]];
NSLog(#"Player Should play!");
[player play];
}
}
You need to set the frame on player's view.
[[player view] setFrame: [self.view bounds]];
i have a tabbar based application with 2 tabs. One to show images and the other to show videos. If i click on the video tab, different thumbnails are showing up. If i click on one of these images, i would like to push that videoviewController to play a video in another navigation view - if i turn the iphone around, it will play fullscreen. So, actually its the same functionality like the original "photo"- app.
What i´ve got so far is a scrollview with different buttons where i added a backgroundimage. By clicking on one of these buttons, i´ve added the button-functionality:
[myButton addTarget:nil action:#selector(buttonDown: ) forControlEvents:UIControlEventTouchUpInside];
And here´s my "buttonDown"-method:
-(void) buttonDown:(NSString*) sender {
moviePlayer = [[MoviePlayer alloc] init];
[self.view addSubview:moviePlayer.view];
NSLog(#"MoviePlayer: %#", moviePlayer);
[self.navigationController pushViewController:moviePlayer animated:YES];
[moviePlayer release];}
Here´s my "MoviePlayer.m"-file:
- (void)viewDidLoad {
UIView* imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100 )];
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];
[imageView release];
NSString *url = [[NSBundle mainBundle] pathForResource:#"testmovie" ofType:#"mp4"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
//player.scalingMode = MPMovieScalingModeAspectFill;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player autorelease];
}
So the navigation works great - by clicking on a button, the view is pushed (and i get my green test-uiview) and the sound of that video file is been playing - but no video is visible, only that sound.
I´ve no idea - if i test that code in a new viewbased-project-template it works great. So i think there some problems with the views.
Any ideas on that?
Would be great. Thanks for your time.
Give this a go:
[player setFullscreen:YES];
[self presentMoviePlayerViewControllerAnimated:player];
I believe that should resolve your woes! Please vote for answer if it assists!