MPMoviePlayerController not playing the video - iphone

I want to play videos in iphone app and i am using MPMoviePlayerController. It used to work fine but suddenly when i tried to integrate the viewcontroller class which plays the video, it stopped playing. I tried to import the MediaPlayer Framework and did a lot of R&D stuff but still its not working. I am using this code..
NSURL *fileURL = [NSURL URLWithString:xmlParser.videoUrlLink];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
//[moviePlayerController setControlStyle:MPMovieControlStyleEmbedded];
[self.moviePlayerController.view setFrame:CGRectMake(5,65,307,200)];
[self.view addSubview:self.moviePlayerController.view];
[self.moviePlayerController play];
I am getting a warning and the method moviePlayBackDidFinish: is getting called repeatedly even before the video is played. The warning is
An instance 0x168da0 of class AVPlayerItem was deallocated while key value observers
were still registered with it. Observation info was leaked, and may even become
mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop
here in the debugger. Here's the current observation info: ( Context: 0x0, Property:
0x10b570> Context: 0x0, Property: 0x117ab0>
Can someone please help me on how to get out of this....It would be of great help....

Related

MPMoviePlayerController playback terminates before video finishes

I'm having trouble understanding this class and getting it to work properly, here is the piece of code where I use it:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:_videoURL];
UIImage *videoThumbnail = [moviePlayer thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame];
[lastImageView setImage:videoThumbnail];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setShouldAutoplay:YES];
[moviePlayer prepareToPlay];
[moviePlayer.view setFrame:lastImageView.frame];
moviePlayer.view.transform = CGAffineTransformMakeRotation((90*M_PI)/180);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
The only reason why the videoThumbnail line is still there is because i didn't get the video to play until I was just trying it out to see if it would get the image from there and then it suddenly began to work... sort of.
Now it plays for 2-3 secs and then terminates without sending MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerPlaybackStateDidChangeNotification
I googled around a bit and couldn't find any useful tips, could someone tell me what's wrong or what i am forgetting
If you're not assigning the newly created MPMoviePlayerController instance to anything other than a variable with local scope (moviePlayer), then the movie player will be deallocated before the movie gets playing. (I imagine the thumbnailImageAtTime call keeps it around for a bit longer.)
Try assigning the movie player instance to a retained (strong) instance variable or property. Of course it should be released when finished, as multiple movie player instances don't play well together.
Also, note that, as of iOS 5, calling prepareToPlay is required. The following is from chapter 28 of Matt Neuberg's Programming iOS 5, Second Edition:
Before you can display a movie in your interface with an MPMoviePlayerController, you must call prepareToPlay, which is supplied through the MPMediaPlayer protocol (adopted by MPMoviePlayerController). This requirement is new in iOS 5, and is a major difference from previous versions of the system; your old code can break if it didn’t make this call.

MPMoviePlayerViewController can not play mp4 file

I'm working on a test application that will run an mp4 file from internet.
code is :
-(IBAction)playRemoteVideo
{
NSString *mp4File = #"http://archive.org/download/Pbtestfilemp4videotestmp4/video_test_512kb.mp4";
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:mp4File]];
[self presentMoviePlayerViewControllerAnimated:playerController];
playerController.moviePlayer.movieSourceType=MPMovieSourceTypeStreaming;
[playerController.moviePlayer play];
[playerController release];
playerController=nil;
}
When I run the application and played the video the player tries to load the video for a while but after I got this exception on console
2012-04-18 22:45:11.309 VideoPlayer[891:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem can occupy only one position in a player's queue at a time.'
*** First throw call stack:
(0x1df1052 0x1333d0a 0x27cfb31 0x27cbb2a 0x27e45cc 0x103b73 0xd4e6a 0x2ff2445 0x2ff44f0 0x1d28833 0x1d27db4 0x1d27ccb 0x16d8879 0x16d893e 0x24ea9b 0x1d12 0x1c85)
terminate called throwing an exception(gdb)
If I execute the same code with an m3u8 file , for instance;
http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
I can get the video running but same does not work for mp4 file.
Do you have any idea why I got this exception and what's wrong with my code?
I run the application on Iphone simulator and I have XCode 4.2
Best Regards
Tugrul
Set the movie player's control style like so.
[self.mPlayer.moviePlayer setControlStyle:MPMovieControlStyleDefault];
Also, local files have the MPMovieSourceTypeFile, not MPMovieSourceTypeStreaming property set.
One more scenario where this can happen is, as the log quite helpfully tells you, when "An AVPlayerItem can occupy only one position in a player's queue at a time." Basically, when you're trying to get two videos to start playing at the same time, or even INTERACT with two videos/MPMoviePlayerController objects at the same time.
In my app I use two MPMoviePlayerController's and keep swapping them around to create the illusion for the user of infinitely moving and swapping between different videos.
This was working fine so far, but I recently added notifications for some events that should result in the videos being paused and resumed. However I didn't realize both my player objects were listening to the notifications at the same time, and hence tried to trigger either "Pause" or "Play" at the same time. This caused the framework to think I was trying to play multiple videos at the same time, and threw this exception.
All I had to do now was make sure that only one player object was listening to notifications at any given point. Just a small tweak in my application logic.
So if you're getting a weird error, it need not be the framework that has a problem, a malicious operating system bent on making your life hell or an act of god. It could just be pure, good old-fashioned bad code. :)
I know this error. Try this code here.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
self.movieController = player;
self.movieController.fullscreen = YES;
self.movieController.controlStyle = MPMovieControlStyleDefault;
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.movieController];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerDidExitFullscreenNotification
object:self.movieController];
[self.movieController prepareToPlay];
[self.movieController.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.movieController.view];
// ...
[self.movieController play];

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

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.

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