1
votes

i am an amateur ios developer with version 4.6 of xcode and OSX 10.8.4. I am trying to make an app which plays sound but when i try to run the app i get this message:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* - [NSURL initFileURLWithPath:]: nil string parameter'

My app also wont launch.

Here is the code for my view controller.h:

      ViewController.h:

 #import <UIKit/UIKit.h>
#import <RevMobAds/RevMobAds.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController 



- (IBAction)playButton:(id)sender;
- (IBAction)stopButton:(id)sender;

@end

And here is my view controller.m:

      Viewcontroller.m:


#import "ViewController.h"
#import <RevMobAds/RevMobAds.h>


@interface ViewController ()
{
AVAudioPlayer *avPlayer;
}
@end

@implementation ViewController 
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];

NSURL *url = [NSURL fileURLWithPath:stringPath];

NSError *error;

avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];

[avPlayer setNumberOfLoops:-1];


}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

 }


- (IBAction)playButton:(id)sender {
[avPlayer play];
}

- (IBAction)stopButton:(id)sender {
[avPlayer pause];

}
@end
1
Are you sure that 98648__dobroide__20100606-mosquito.mp3 exists in your bundle? Case sensitivity matters.Mick MacCallum
i copied and pasted itMatthew Olivan

1 Answers

6
votes

The problem is pathForResource is returning nil. Have you added that exact file name to your bundle?

Also, you should be testing for stringPath and error before continuing to load the player. For example:

NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"98648__dobroide__20100606-mosquito" ofType:@"mp3"];
if (stringPath) {
    NSURL *url = [NSURL fileURLWithPath:stringPath];
    NSError *error;
    avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    if (!error) {
        [avPlayer setNumberOfLoops:-1];
    } else {
        NSLog(@"Error occurred: %@", [error localizedDescription]);
    }
} else {
    NSLog(@"Resource not found");
}