i have a problem with MPMoviePlayerController [iPhone SDK] - iphone

i want create an app with movie intro just like gameloft games . so when application has lunched , movie plays fine but before the move plays .. my FirstViewController xib file show first then movie start to play ! why ? here is my code :
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
IntroMovie.movieControlMode = MPMovieControlModeHidden;
[IntroMovie play];
}

You need to wait for the movie to finish playing before adding your first view as a subview to window.
This should do the trick. Change your code to:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Hafez-2" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
[IntroMovie play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}

An alternative is to add a different view (e.g. simple black background) and then replace it when the video has finished playing:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Hafez-2" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *IntroMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[IntroMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
[IntroMovie play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
// Create an initial pure black view
UIView* blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
blackView.backgroundColor = [UIColor blackColor];
[window addSubview:blackView]; // sends [blackView retain]
[blackView release];
[window makeKeyAndVisible];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
UIView* blackView = [[window subviews] objectAtIndex:0];
[blackView removeFromSuperview]; // sends [blackView release]
[window addSubview:viewController.view];
}
Let me know if you need modified source from your example and I can post it somewhere

Related

MPMoviePlayerController not working on iOS8

I was trying out a project I worked on during iOS6. Tried running the same on iOS8 and the video is playing. Is there anything else I need to do?
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
Thanks.
Finally. Adding it to the UIWindow solved the problem for me.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"disc" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
Init your MPMoviePlayerController in .h file like below :
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
After above changes it will work .
Sample Code:
#interface ViewController : UIViewController
#property(strong,nonatomic)MPMoviePlayerController *moviePlayerController;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"Mcdonald" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.allowsAirPlay = NO;
_moviePlayerController.shouldAutoplay = YES;
[_moviePlayerController.view setFrame:[UIScreen mainScreen].bounds];
_moviePlayerController.controlStyle=MPMovieControlStyleEmbedded;
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayerController];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

repeatmode doesn't work in MPMoviePlayerViewController?

How to display a recorded video as repeat as an'n' number of times like in Vine Application.
Here I use the MPMoviePlayerViewController, and works great display the recorded video. But the problem is, it doesn't repeating.
Here the currently using code is,
NSURL *url = [NSURL fileURLWithPath:videoPath];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.view.frame = CGRectMake(200, 402, 300, 200);
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview: playerController.view];
[playerController.moviePlayer play];
NSLog(#"repeatMode: %d",playerController.moviePlayer.repeatMode);
[playerController.view addSubview:customview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerController moviePlayer]];
And the NSNotification code,
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
NSLog( #"myMovieFinishedCallback: %#", aNotification );
MPMoviePlayerController *movieController = aNotification.object;
NSLog( #"player.playbackState = %d", movieController.playbackState );
}
Can anyone please give the solution..
Note:
I'm using the XCode 4.5.2 tool and ios simulater 6.0
Try this code. It is working.
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Video" ofType:#"mp4"]];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
[moviePlayerController.moviePlayer prepareToPlay];
[moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.view addSubview:moviePlayerController.view];
you can try this code
-(void) movieFinishedCallback:(NSNotification *) aNotification
{
if (aNotification.object == self.moviePlayer) {
NSInteger reason = [[aNotification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[playerController play];
}
}
}
You have to keep the MPMoviePlayerController object as a member, otherwise the app would lose context to it and the movie will not loop.

i have videos list when i select row video will play on another view how to stop and come back for table view

if (indexPath.row==0)
{
NSString *urlAsString = [[NSBundle mainBundle] pathForResource: [self.array objectAtIndex:indexPath.row] ofType:#"mov"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:urlAsString];
[self.view addSubview:myPlayer.view];
[myPlayer.view setFrame:CGRectMake(0, 0, 320, 400)];
[myPlayer setFullscreen:YES animated:YES];
[myPlayer play];
}
i have one button in MPMoviePlayerController called done when i click on done button i need to come back to my tableview plz help me any one
MPMoviePlayerController *myPlayer declare global and init . and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:nil];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[myPlayer stop];
}

How can i play video by getting Video from PhotoLibrary

I am doing an app on ipad,Which will capture video and it will save in photo Library. But what i want is i want to play that video choosing from the photo library and need to play over there.I saw many examples using MPMoviePlayerController but all i saw is they adding video over there and they playing that video.Is there any way to write path for my below mentioned code.
My code goes here
This where i calling Photo library
-(IBAction) goToPhotos:(id)sender {
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.allowsEditing = YES;
UIPopoverController *videoController = [[UIPopoverController alloc]initWithContentViewController:ipc];
// pop.popoverContentSize = CGSizeMake(300, 900);
[videoController presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[ipc release];
[self presentModalViewController:ipc animated:YES];
}
Here i am calling MPMoviePlayerController .Code goes here
- (void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperview];
[player autorelease];
}
Can any one tell me how can i get path for photolibrary and i need to play video over there.
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
Can we modify this line and is there any way that i can get path to photo library so that i can play video over there. Help me Thanks!!
You need to implement the delegate methods for UIImagePickerController.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
//finish implementing the movie player controller
}

iPhone SDK: Stopping a Video

How can I stop a video once I navigate away from a view and make the video play again when I get back to the original view again?
Here is the code to play the video in the original view:
- (void)viewDidLoad {
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Video" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
theMovie.view.frame = CGRectMake(104.0, 134.0, 200.0, 250.0);
[self.view addSubview:theMovie.view];
[theMovie play];
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
}
Make theMovie instance var
Stop playing in the -(void) viewWillDisappear:(BOOL)animated
And it should start playing when you navigate back to this view. Or move this code to -(void) viewDidAppear:(BOOL)animated