I'm having problems playing a movie with the new xcode that comes with iOS 5 beta. I created a simple project (with storyboards and all), and added this code to a button:
MPMoviePlayerController *moviePlayer;
NSString *path = [[NSBundle mainBundle] pathForResource:@"position" ofType:@"m4v"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
//[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[moviePlayer.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
The confusing thing is that it works perfectly when I throw the code into a project built with a previous version of xcode (specifically the one that comes with iOS 4.3).
Any ideas?
New to Objective-C but I'll give a shot at it. With Xcode 4.2 (which offers to code for iOS5), by default, new projects have ARC (Automatic Reference Counting) turned on. For the code you gave, since you declare the moviePlayer inside this block of code, moviePlayer automatically gets released when it steps out of the block. In older projects, moviePlayer would just linger on, potentially creating a memory leak. I got it working in with the default Xcode 4.2 ARC setting turned on by declaring moviePlayer in the header file of the class, which means it gets released only when the object instance of that class is released.
MPMoviePlayerController *moviePlayer;
put it on header (.h) file and its work
I found the solution in this code:
movieView = [moviePlayer view];
[movieView setFrame: CGRectMake(0, 0, 1024, 768)];
Try this..it works in iOS 5. You can edit the code to have it play with an IBAction button. Good luck
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface Playing_Video_FilesViewController : UIViewController
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@property (nonatomic, strong) UIButton *playButton;
@end
- (void) startPlayingVideo:(id)paramSender{
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:@"Sample"
ofType:@"m4v"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if (self.moviePlayer != nil){
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(videoHasFinishedPlaying:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
NSLog(@"Successfully instantiated the movie player.");
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer play];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES
animated:YES];
} else {
NSLog(@"Failed to instantiate the movie player.");
}
}
- (void) stopPlayingVideo:(id)paramSender {
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[self.moviePlayer stop];
if ([self.moviePlayer.view.superview isEqual:self.view]){
[self.moviePlayer.view removeFromSuperview];
} }
}
- (void) viewDidUnload{
self.playButton = nil;
[self stopPlayingVideo:nil];
self.moviePlayer = nil;
[super viewDidUnload];
}
- (void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
/* Find out what the reason was for the player to stop */
NSNumber *reason =
[paramNotification.userInfo
valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if (reason != nil){
NSInteger reasonAsInteger = [reason integerValue];
switch (reasonAsInteger){
case MPMovieFinishReasonPlaybackEnded:{
/* The movie ended normally */
break; }
case MPMovieFinishReasonPlaybackError:{
/* An error happened and the movie ended */
break;
}
case MPMovieFinishReasonUserExited:{
/* The user exited the player */
break;
}
}
NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);
[self stopPlayingVideo:nil];
} /* if (reason != nil){ */
}
// link the method to a button and it will work in iOS5
-(void)playMovie
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"MOVIENAME" ofType:@"MOV"]];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
I had a similar issue on iOS 5 with MPPlayerController and I have checked Apple's example project, the difference was only setting frame so I set frame manually and it worked perfectly.
[[[self moviePlayer] view] setFrame:CGRectMake(0, 0, 320, 480)];