Here is the code that I wrote to play a video in my Xcode project. When I run the project it crashes on me. I get this reason: Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[NSURL initFileURLWithPath:]: nil string parameter’
I’ve found a tutorial that works when I run it on the simulator.
I added my video to the project and updated the code accordingly. My video is in a m4v format just like the other video in the tutorial. When I run the app with my video it still crashes and gives me the same error. I took my video from quick time and exported it over to iTunes. What am I doing wrong?
.h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface BigBuckBunnyViewController : UIViewController {
}
-(IBAction)playMovie:(id)sender;
@end
.m file
#import "BigBuckBunnyViewController.h"
@implementation BigBuckBunnyViewController
-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender;
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ladder rack 1 4"
ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]
initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x,
playButton.frame.origin.y,
playButton.frame.size.width,
playButton.frame.size.height)];
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
//moviePlayerController.scalingMode = MPMovieScalingModeFill;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
- (void)dealloc {
[super dealloc];
}
@end
Ok so I was able to create an html page and link the html page to my .m file in the view did load section. I created an UIWebview and added it to my xib view. I placed my video on a server. This is probably a better solution, less load time.
You are geting nil because the
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ladder rack 1 4" ofType:@"m4v"];contains spaces.So you need to change it like:
Refer this link