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

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

Related

MPMoviePlayerController not playing the video

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....

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];

MPMoviePlayerController not firing notifications in iPhone 5 Simulator

I ran into a problem... perhaps someone bumped into something similar: I have an application that uses a MPMoviePlayerController, and used to work perfectly well.
Trying to compile and run it with new Xcode 4.2 using iPhone 5 Simulator, MPMoviePlayerController is not sending notifications when I load a movie. Looks like it fails to properly detect the video file.
The code looks more or less like that (simplified code):
// First I'm initializing the player with a URL from a file
MPMoviePlayerController *player;
player = [[MPMoviePlayerController alloc] init];
player.shouldAutoplay = NO;
NSURL *url = [NSURL fileURLWithPath:path];
player.contentURL = url;
// Then I'm adding an observer in order to wait for the player to find movie duration
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(durationAvailable:)
After the above, I'm waiting in a loop for the notification to get called, by waiting for about 10 seconds, during which I'm calling the run loop.
Using the same code, same Xcode 4.2 version, but with iPhone 4.3 Simulator, after about a second the notification gets called, and I can read the player duration. However, when running the exact same code on iPhone 5 Simulator, the notification function never gets called, and if I try to read the movie duration afterwards it contains 0.
Any idea?
Thanks,
Ariel
OK, I managed to find a workaround to the problem.
Apparently, adding this line after adding the observer does the trick:
[player pause];
It looks like if you don't "activate" the player somehow, the notifications will not be fired. In my case, I just wanted to get a notification for the duration of the movie without starting a playback, and it worked well in versions prior to iOS5, but it looks like they changed the behavior. Anyway, the above line solves the problem.
Ariel
try this MPMoviePlayerPlaybackStateDidChangeNotification :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(stop)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];

Can't play mp4 in Cocoa-Touch App

I am trying to play an mp4 after detecting a signal in the audio jack.
The video is playing once, after a delay of 1 sec which i dont want, and then application is freezing and i get this warning on the debugger: (only on the iphone, its working on simulation)
(8F190)/Symbols/System/Library/VideoDecoders/H264H4.videodecoder (file not found).
(8F190)/Symbols/System/Library/VideoDecoders/MP4VH4.videodecoder (file not found).
my code for the video is this :
//play video1
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sample1" ofType:#"mp4"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.useApplicationAudioSession=NO;
[moviePlayer prepareToPlay];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
I couldnt find anything on this warning on the net.
I have tried any kind of video encoding,according to the Apple docs.
We just cant play movies, does anyone have any idea how to fix it?
thanks .
If you run that code twice, the video won't play the second time, because you are creating a second MPMoviePlayerController and a second view, and only one MPMoviePlayerController view in your app can play video. So it works the first time but not the second time. You should be retaining your MPMovieVideoController in a property so that you can remove its view and release the MPMovieVideoController before trying to make a new one.

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.