issue in picking video and playing it - iphone

i being trying to play video from lib but i cant figure out what im doing wrong i have tried many things and its not showing any kind of error my code is below
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
NSURL *url = [[NSURL alloc]initWithString:mediaType];
NSLog(#"%#",url);
[self dismissModalViewControllerAnimated:NO];
/* Create a new movie player object. */
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
if (player)
{
/* Specify the URL that points to the movie file. */
[player setContentURL:url];
/* Add a background view as a subview to hide our other view controls
underneath during movie playback. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[player view] setFrame:viewInsetRect];
[player view].backgroundColor = [UIColor lightGrayColor];
/* To present a movie in your application, incorporate the view contained
in a movie player’s view property into your application’s view hierarchy.
Be sure to size the frame correctly. */
[self.view addSubview: [player view]];
[player setCurrentPlaybackRate:0.5];
[player play];
}
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
}
// When the movie is done, release the controller.
-(void) myMovieFinishedCallback: (NSNotification*) aNotification
{
MPMoviePlayerController *playerr = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:playerr];
[playerr stop];
[self.view removeFromSuperview];
[playerr autorelease];
}
what happen is first presetModelViewController is getting displayed then i can shows videos which are in my lib i can select one of them but when i choose video then it shows my grey color view and nothing else and my video is not getting played.
If you could find any errors in my code and can help me with this then it will be great help.

Please go through the below link will help you with your existing issue and also implementation.
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];

This is what solved my problem thanks to spider1983
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[self dismissModalViewControllerAnimated: YES];
}
// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
NSLog(#"%#",[info objectForKey:
UIImagePickerControllerMediaURL]);
/* Create a new movie player object. */
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[info objectForKey:UIImagePickerControllerMediaURL]];
if (player)
{
/* Specify the URL that points to the movie file. */
[player setContentURL:[info objectForKey:
UIImagePickerControllerMediaURL]];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
kMovieViewOffsetX,
kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[player view] setFrame:viewInsetRect];
[player view].backgroundColor = [UIColor lightGrayColor];
/* To present a movie in your application, incorporate the view contained
in a movie player’s view property into your application’s view hierarchy.
Be sure to size the frame correctly. */
[self.view addSubview: [player view]];
[player prepareToPlay];
[player setCurrentPlaybackRate:0.5];
[player play];
}
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: player];
}
}

Related

how to play a video on startup of the app or on viewdidload with done button?

I have been able to successfully play a video on start of the app by calling a function that plays the video in the viewDidLoad method. But on doing so, i am unable to see the "Done" button. The "Done" button only seems to appear if i call that same function with the help of a button. I am posting the code below. How will I make this done appear by calling the same function in the viewDidLoad method? What have i been doing wrong? Thank you!
in my viewcontroller.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMedia];
}
- (void) playMedia {
//hide status bar first
[[UIApplication sharedApplication] setStatusBarHidden:YES];
//resets again in playMediaFinished
movieFile = [[NSBundle mainBundle] pathForResource:#"sec" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath: movieFile];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setFullscreen:NO animated:YES];
[self.view addSubview:moviePlayer.view];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
CGAffineTransform transform = self.view.transform;
[moviePlayer.view setFrame:CGRectMake(0, 0, 480, 320)];
moviePlayer.shouldAutoplay = YES;
NSLog(#"Should be playing Movie");
}
- (void) playMediaFinished: (NSNotification*) theNotification {
moviePlayer = [theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
if ([moviePlayer
respondsToSelector:#selector(setFullscreen:animated:)])
{
[moviePlayer.view removeFromSuperview];
[playerView removeFromSuperview];
//reset status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
NSLog(#"Should be DONE playing Movie");
}
Try doing this using viewDidAppear instead of viewDidLoad. I'm not surprised starting a video in viewDidLoad is causing unexpected behavior considering you're presenting the video before the main view appears on screen. Keep in mind, the view is done loading before it is actually visible.

Playing two videos after another causes short black screen

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.

How do you play a movie in an iphone app?

I want to add a movie at the beginning of my iphone app. I have watched the tutorials and this is the code I have come up with. Why won't it work. I don't even see a movie. I call it from the viewDidLoad function of my ViewController with the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"BTS Intro" ofType:#"mov"];
[self playMovieAtURL:[NSURL fileURLWithPath:path
-(void)playMovieAtURL:(NSURL *)theURL {
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
thePlayer.scalingMode = MPMovieScalingModeAspectFill;
thePlayer.controlStyle = MPMovieControlStyleNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification *)aNotification {
MPMoviePlayerController *thePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer release];
}
You initiated playing of a movie but didn't add [thePlayer view] into views hierarchy that is why you do not see anything. Take a look at description of this operation in apple docs:
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.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
MPMoviePlayerController Class Reference

playing intro movie on ipad

i am trying to play an intro movie (like a splash screen). it used to work fine for sdk 3.0 but now i am doing it for iPad. and its not running movie instead it just show black screen before view appears.
here is my code
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"m4v" inDirectory:nil];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.fullscreen=YES;
moviePlayer.view.frame = CGRectMake(0, 0,1024,768);
[moviePlayer setShouldAutoplay:YES];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setRepeatMode:MPMovieRepeatModeNone];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
return YES;
}
and here is my moviePlaybackDidFinish
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
theMovie.initialPlaybackTime = -1;
[theMovie pause];
[theMovie stop];
[theMovie release];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
i dont know what i am missing here?
thanks for any help....
UPDATE i just found out that it plays sound but no video....is this obvious?pls help
As you're using MPMoviePlayerController over MPMoviePlayerViewController I think you need to be adding the player's view to your view hierarchy before calling play, as per the docs:
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view]; //add the player' view
this might explain why you're hearing audio but not seeing anything, pretty sure you need to setup the app window first too ([window makeKeyAndVisible]; etc)

Want to play video but there´s only sound, no visible content

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!