How do I display video from AppDelegate in iOS4 - iphone

Now I think I know about the differences between 3.X and 4 with regards the MPMoviePlaybackController and the need to set the view and have it fully working in a child view controller. But despite the following code seeming correct (to me) I still just get a blank screen for the duration of the movie. I know it plays successfully as moviePlayBackDidFinish fires.
Do I need to add it to a modal or similar at this point?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
player.fullscreen = YES;
player.controlStyle = MPMovieControlStyleNone;
[[player view] setFrame:window.bounds];
[window addSubview: [player view]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}

the MPMoviePlayerController does NOT have a view property.
you should/must use MPMoviePlayerViewController instead.
here's what I do:
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:currentChannel.StreamURI]];
[moviePlayerViewController.moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer setScalingMode: MPMovieScalingModeAspectFit];
moviePlayerViewController.view.frame = CGRectMake(0, 0, 480, 320);
[self.view addSubview:moviePlayerViewController.view];
note that you might want to change movieSourceType to MPMovieSourceTypeFile or MPMovieSourceTypeUnknown.
The above code is 100% of what I need to play a Movie (in my case a streaming channel)

In my case I had moved the
[window makeKeyAndVisible];
Out from
didFinishLaunchingWithOptions
and into my
movieDidFinish
By putting it back it worked

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.

Play movie from didFinishLaunchingWithOptions

my question is: I have a short introduction movie for my application and i want to show it when the application launches, the problem is that im not having any success.
Im using this code on the "didFinishLaunchingWithOptions" method:
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"launch" ofType:#"mov"]]];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.view.frame = _window.bounds;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[_window addSubview:moviePlayer.view];
[_window makeKeyAndVisible];
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer play];
Best Regards
What you should do instead is out that code in your RootViewController's viewDidAppear method. Or whatever other view controller is displayed. That kind of thing doesn't belong in the application delegate and you also shouldn't be adding subviews directly to your UIWindow instance.
Put the code in a view controller and add the MPMoviePlayerController's view as a subview to self.view and it should work fine.

IPhone Frame Video Playing

I have written this piece of objective-c code for playing a video after touching an image.
//start video here
NSString *path = [self localVideoPath:NO];
// Create custom movie player
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithPath:path] autorelease];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(onMSAASDone:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[moviePlayer setFullscreen:FALSE];
//---play partial screen---
moviePlayer.view.frame = CGRectMake(0, 0, 200, 300);
//[[moviePlayer view] setFrame: [image bounds]];
[self.view addSubview:moviePlayer.view];
// Show the movie player as modal
//[self presentModalViewController:moviePlayer animated:YES];
// Prep and play the movie
[moviePlayer play];
The problem is that the code stops working at second line of statement. The error is... Program GDB: Received Signal: "EXC_BAD_ACCESS"
Help Me Please!
MPMoviePlayerController *moviePlayer = [[[MPMoviePlayerController alloc] initWithPath:path] autorelease];
the function initWithPath is not the part of MPMoviePlayerController class,
i could not find it in documentation.
May be you were trying to use
- (id)initWithContentURL:(NSURL *)url
plz read the apple documentation.
http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

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!