Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video playback fails - [NSURL initFileURLWithPath:]: nil string parameter

I'm struggling to understand why this isn't working :/ Everytime I run the project the app crashes throwing me a 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

I've followed a tutorial (I'm pretty new to this) and it worked for him and the code is exactly the same.. Can anyone explain whats going on?

.h file

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <QuartzCore/QuartzCore.h>

@interface FirstViewController : UIViewController {   
    MPMoviePlayerViewController *playerController;
}
-(IBAction)playVideo;
@end

.m file

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

{
    MPMoviePlayerController *mpc;

}



- (IBAction)playButton:(id)sender {

    NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"intro" ofType:@"MP4"];
    NSURL *url = [NSURL fileURLWithPath:stringPath];

    if(url != nil){

    mpc = [[MPMoviePlayerController alloc]initWithContentURL:url];

    [mpc setMovieSourceType:MPMovieSourceTypeFile];

    [[self view]addSubview:mpc.view];

    [mpc setFullscreen:YES];

    [mpc play];

    }
    else{
        NSLog(@"URL not found");

    }
}
@end
like image 744
Jordan Earle Avatar asked Jan 06 '13 02:01

Jordan Earle


3 Answers

The only important part of all that is:

NSString *stringPath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"MP4"];
NSURL *url = [NSURL fileURLWithPath:stringPath];

I assume the exception is raised inside the second line. Which would mean that stringPath was nil, which would happen if the file intro.MP4 was not actually in your app bundle.

Check that:

  • the file exists in your copy of the source code

  • your Xcode project has a reference to that file (if it's red, it means the file isn't actually present)

  • In your target in Xcode, look at "Build Phases" and reveal the "Copy Bundle Resources" build phase. That file should be present. If it isn't, press the + button and select it.

like image 184
Kurt Revis Avatar answered Nov 11 '22 12:11

Kurt Revis


Kurt's answer worked for me specifically the

"In your target in Xcode, look at "Build Phases" and reveal the "Copy Bundle Resources" build phase. That file should be present. If it isn't, press the + button and select it."

my movie file was missing. It must have been there before because it was working and then it just stopped.

I would have made a comment to Kurt's post but did not know how. I did "up vote".

like image 29
arnjmllr Avatar answered Nov 11 '22 11:11

arnjmllr


Make sure you add the audio files you are trying to play in Copy Bundle Resources.

  1. Click on Xcode Project Icon(First one in the Left Project Navigator)
  2. Click on Target
  3. Tap on Build Phases
  4. Go to Copy Bundle Resources
  5. Check if your audio files are available in the list.
  6. If not added, Add them by tapping on the + icon
  7. Build and Run
like image 1
Pradeep Reddy Kypa Avatar answered Nov 11 '22 11:11

Pradeep Reddy Kypa