8

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
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Jordan Earle
  • 81
  • 1
  • 1
  • 2

6 Answers6

34

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.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • Thanks for replying. Unfortunately it still isn't launching and I made sure of everything you recommended. intro.mp4 is now definitely in the bundle? I'm very confused to why this is not launching – Jordan Earle Jan 06 '13 at 10:43
  • 1
    Also check that you've spelled the name correctly (including case -- are you sure it's MP4 not mp4?). You may also want to look inside of your built app, on disk, to see if the file is where it should be. – Kurt Revis Jan 06 '13 at 10:47
  • I've checked the .app file and the video is inside in the main folder where it should be? Also tried changing case on the MP4 but that didn't make a difference apparently :( – Jordan Earle Jan 06 '13 at 11:06
  • hey working perfectly when i added video file in build phases..thanks – Iftikhar Ali Ansari Dec 03 '14 at 12:45
6

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

arnjmllr
  • 93
  • 4
1

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
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
0

In addition what's mentioned in this answer https://stackoverflow.com/a/14179186/3213411, you also want to make sure that the file you are referencing has membership in the build target. You can specify this in the File Inspector. This answer explains how https://stackoverflow.com/a/5300901/3213411

Community
  • 1
  • 1
Matt Lathrop
  • 16
  • 1
  • 3
0

Your video is too long. Using this code, the limit is 30 seconds. Your best bet is to stream it from a url. Try playing a video shorter then 30 seconds and it will work fine.

msweet168
  • 371
  • 1
  • 18
0

iOS 9.2, Xcode 7.2, ARC enabled

I recently ran into this issue myself. Definitely do what the original contributors are suggesting. For me it was the actual name of the file that was the issue, see below...

NSString *pathForPuzzlePieceHitSound = [[NSBundle mainBundle] pathForResource:@"puzzle_piece_hit" ofType:@"m4a"];
NSURL *urlForPuzzlePieceHitSound = [NSURL fileURLWithPath:pathForPuzzlePieceHitSound];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)urlForPuzzlePieceHitSound, &idForPuzzlePieceHitSound);

The file name:

@"puzzle_piece_hit"

Has characters in it that are illegal for file naming using this particular method. I guess you can treat it and get around the issue, but it is easier to just rename the file.

After I renamed to:

@"puzzlePieceHit"

Everything worked just fine.

Hope this helped someone! Cheers.

serge-k
  • 3,394
  • 2
  • 24
  • 55