Reputation: 29935
I'm probably missing something really obvious.
I've included <MediaPlayer/MediaPlayer.h>
and have this code:
NSURL *videoURL = [NSURL fileURLWithPath:pathToFile];
MPMoviePlayerViewController *mediaPlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:mediaPlayer];
mediaPlayer.view.backgroundColor = [UIColor blackColor];
[mediaPlayer release];
But the video won't play. I copied the code from another place where the video works perfectly.
pathToFile
is correct becuase the variable is used in previous lines to move the video from the resources folder to the documents directory.
Any ideas why it might not be working?
Thanks
Upvotes: 0
Views: 392
Reputation: 12787
You can use following code except to presentMoviePlayerViewControllerAnimated
NSString *movieFile=[[NSBundle mainBundle] pathForResource:@"movie" ofType:@"m4v"];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = self.view.frame;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
this works fine for me.
Upvotes: 2
Reputation: 20586
I don't think presentMoviePlayerViewControllerAnimated
retains its receiver, so it looks as though you're releasing your movie player too early. You could try making mediaPlayer a retained property:
@interface MyClass : SuperClass {
MVMoviePlayerViewController *mediaPlayer;
}
@property (nonatomic, retain) MVMoviePlayerViewController *mediaPlayer;
@end
@implementation MyClass
@synthesize mediaPlayer;
// rest of class implementation here...
@end
Then initialise as so:
self.mediaPlayer = [[[MPMoviePlayerViewController alloc]
initWithContentURL:videoURL] autorelease];
And release afterwards with:
self.mediaPlayer = nil;
(To write code that happens after the video has finished playing, check out the MPMoviePlayerPlaybackDidFinishNotification
notification.)
Also bear in mind that presentMoviePlayerViewControllerAnimated
first appeared in iOS 3.2, so this code won't work on earlier iOS versions. But I don't think that's the problem in this case.
Upvotes: 2