Video not showing in iPhone Simulator (but I can hear audio from the video) - iphone

I am new to xcode, interface builder, and the iphone simulator. I am trying to play a movie/video in xcode on the click of a button (which is very straightforward). I am using xcode 3.2.4 and iphone simulator 4.1.
When I launch the iPhone simulator and click the button to launch the video, the audio plays, but the video is hidden. It's as if the video is behind the tab bar (this is part of a tab bar application). I am not sure how to make the video play in front.
Here's the code:
-(IBAction)launchVideo2:(id)sender{
NSString *movieFile;
MPMoviePlayerController *moviePlayer;
movieFile = [[NSBundle mainBundle]
pathForResource:#"IGDIs_Video_Picture_Naming_iPhone" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath: movieFile]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayer play];
}
-(void)playMediaFinished:(NSNotification*)theNotification
{
MPMoviePlayerController *moviePlayer=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
}
Any suggestions would be great!

should be using the MPMoviePlayerViewcontroller
see apple documentation
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html

It doesn't look like you have added the movie view to your main view. You'll need to add something like:
...
[[moviePlayer view] setFrame:[self view] bounds]];
[[self view] addSubview:[moviePlayer view]];
[moviePlayer play];
}
before playing it. I'm assuming that the a launchVideo2 method is in a UIViewController that owns the main view. You might have to change [self view] to suit your code to correspond to the view where you want the movie to play.

In my experience, video won't display in the sim. However, audio will. (Which is an improvement, since previously neither would play.)
Try running the app on the device. If the video doesn't display there, post here and we'll work through the issue.

You have a probably unrelated memory issue there:
In your first method, you don't release the player, which is a local variable.
In your second method, you release the player, that you don't own.
Proper memory management (ie best practice), you would do this with an ivar.

Related

more than one MPMoviePlayer in a view?

I need to display more than one movieplayer in a view.
I know I can only play one video.
My problem is that only one of the two MPMovieplayer, I have added to the view, shows me the control buttons.
What can I do to solve my problem?
I use this code to add a movieplayer:
MPMoviePlayerController * moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerPlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerDidEnterFullscreen:)
name:MPMoviePlayerDidEnterFullscreenNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerDidExitFullscreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:moviePlayer];
moviePlayer.view.frame = rect;
[movie setContentURL:contentURL];
[movie prepareToPlay];
[self addSubview:moviePlayer.view];
I think its not possible. Please see the answer for this question
Playing Multiple Videos on iPAD
You will have to use AVPlayer for multiple video viewing, see this
Here is another tutorial for AVPlayer
You can also check this from apple developer portal, link
This question also says something about adding controls to AVPlayer
BR, Hari
Hi this is not possible to play more than 1 video in ios because only one thread at a time .
I face same issue in my iPad application but I've found an alternate solution.
I want to play more video in 1 view controller , but these videos are 4 to 5 sec's only.
I make number of continuous image frames of that video's and make .gif files and show this in a UIWebView. it works fine.

View sizing after playing movie in fullscreen mode

I have a fairly standard navigation-based iPhone app I'm developing, where on some screens, there is a button that will play a movie using an MPMoviePlayerController in fullscreen mode. There is a navigation bar at the top of the view. I'm seeing some odd behaviour with the view sizing after the movie plays. Was wondering if anybody else has seen this, and whether there is a fix. Here is what I'm seeing:
I hold the phone in one orientation (portrait or landscape)
I start the movie (it plays in fullscreen mode)
I tap the screen to hide the movie playback controls
I rotate the phone to another orientation (portrait to landscape, or vice-versa)
I tap the screen to bring back playback controls, and then tap the 'Done' button
At this point, the original view comes back on screen, in the new orientation. However, the view is not sized properly to accomodate the status bar at the top of the screen. The status bar obscures part of the navigation bar. Rotating the phone at this point corrects the problem.
If I follow the same sequence, but without hiding the playback controls, everything works just fine, and the original view is sized properly when I dismiss the movie.
Anybody have any clue what might be causing this?
Also, this seems to be pretty easy to reproduce. I'm using Xcode4.2 and the 5.0 SDK. If you create a new iPhone-only project using the "Master-Detail Application" template, and make these changes, you can see the problem happen:
First, add the following code to MasterViewController.m.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
- (void) playStory
{
NSURL *movieURL = [NSURL URLWithString:#"http://double-apps.com/thisisatest.m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
Then, replace the contents of the didSelectRowAtIndexPath method with [self playStory]:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self playStory];
}
That's it, run the app, hit the 'Detail' entry in the main screen, then follow the sequence listed above.

Play videos fullscreen in landscape (iPhone)

I have an app that is built as a hierarchy of viewControllers.
On of the view Controllers is for a 'video section' of the app.
The app is designed to be portrait only, however, I want to force the videos to play fullscreen in landscape (just like the iPod app on the iPhone).
After searching around, I see that many people have this problem.
I finally have been able to rotate it, but it doesn't work in full screen, that defaults to portrait.
And since it doesn't work in full screen, you see elements in the parent views over the video.
Is there an easy way to rotate this video in full screen, or do I have to broadcast a message to the parent views to hide elements when the video is playing?
here is the code:
NSURL *url = [NSURL URLWithString:[recipeData objectForKey:#"videoPath"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
moviePlayer.shouldAutoplay = YES;
moviePlayer.view.frame = [[UIScreen mainScreen] applicationFrame];
moviePlayer.view.transform = CGAffineTransformMakeRotation(1.57079633);
moviePlayer.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
[self.view addSubview:moviePlayer.view];
//commenting out the line below will rotate the video, leaving it uncommented forces it to play fullscreen in portrait
[moviePlayer setFullscreen:YES animated:NO];
I put the method to create the video in the appDelegate and target it whenever the video is launched. This did the trick, keeping it above everything else.
Have you tried using MPMoviePlayerViewController instead? it presents a full screen almost modal view Controller to handle the video playing. I use it in >=3.2 version and use a hacked up version of MPMoviePlayerController for <3.2
For full screen playback you should use MPMoviePlayerViewController and then to make it launch and play in landscape format use the "shouldAutorotateToInterfaceOrientation" method on the MPMoviePlayerViewController class.
Looks like this:
[yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];

Why does MPMoviePlayerController work in the simulator, but not the device?

I'm having trouble getting MPMoviePlayerController to work on the device. It runs fine in simulator, playing a five-second video and then sending the appropriate callback (myMovieFinishedCallback:) to the view controller. When it runs on the device, the movie never shows up, even though I can trace through [player play] with no problems. myMovieFinishedCallback: is never called, and there are no errors. All the right resources are being copied. Any idea what's going on?
Here's the code I use to create and use the player,
Update: I've switched over to using MPMoviePlayerViewController. On the device the movie still does not play, I just get the spinning progress wheel indefinitely. The controls also flash briefly even though I've set the player to MPMovieControlStyleNone - anybody know how I can fix this? The movie is five seconds and about 1.5 MB if that makes any difference.
Update: Other movie files work, but I can't figure out how to make mine work. I've tried it as a .mov and a .mp4 and the settings seem to be right. Any idea what would cause the MPMoviePlayer to show a progress wheel forever on the device only?
- (void) playMovie
{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"myMovie"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
playerViewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerViewController.moviePlayer.scalingMode = MPMovieScalingModeFill;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
playerViewController.view.frame = movieView.frame;
[movieView addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
}
Your code works fine for me, so there are two possibilities that come to mind (there may be others):
You movieView has not been properly initialized or has not had its frame set so that it's visible.
The video format of the video you're displaying isn't supported on the device.
I would try using a different video. Maybe one you can confirm you've played on the device before. Here's the little demo project I threw together: http://www.cimgf.com/files/PlayMovie.zip

writing an iPhone application with embedded video

I am researching video streaming for an iPhone application that I may have to write in the near future. The application does a whole lot other than stream video, but video aspect is the part that I have no experience with.
Anyone know of any good articles on writing streaming video apps?
Google seems to inundate me with links that have everything not to do what I seek.
Thanks,
m
Apple provide good documentation on the media framework i ntheir docs.
Search for MPMoviePlayerController. The following sample code plays a movie from a URL. (disclaimer, this code lifted from Apple).
-(void)playMovieAtURL:(NSURL*)theURL
{
MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
theMovie.scalingMode=MPMovieScalingModeAspectFill;
theMovie.userCanShowTransportControls=NO;
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
// Release the movie instance created in playMovieAtURL
[theMovie release];
}
I am looking at this issue as well. I would like to embed a video in an iPad app, something like how the Associated Press iPad application handles videos.
Apparently you can do this type of embedded video in OS 3.2 and later. Apple's documentation for MPMoviePlayerController describes how this can be done:
http://developer.apple.com/iphone/library/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html