0

I have 114 audio files named from "0" to "113". Now AVAudioPlayer will load the audio file depends on which row is been selected by the user. For example if user selects row at indexPath 0, "0.mp3" file will be loaded. Now to make it work, i am trying to convert the NSInteger*_selectedRow value into String value and use this String value as audio file path for AVAudioPlayer. But my app gives error of "Nil" value of String Error and get crashed. i might have done something stupid.

This is what i have tried

-(viewDidLoad){
NSString *selectRow=[NSString stringWithFormat:@"%d",_selectedRow];
NSLog(@"Value %@",selectRow);//this is working 
[self audioFilling:selectRow];

}

- (void)audioFilling:(NSString*)value{


    NSError *error;
    // this audioFilePath is getting Nil for pathFor Resource
    NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:value ofType:@"mp3"];
    NSURL *pathAsURL = [[NSURL alloc] initFileURLWithPath:audioFilePath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:pathAsURL error:&error];
    NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle   mainBundle]pathForResource:(NSString*)value ofType:@"txt"]
                                                     encoding:NSUTF8StringEncoding
                                                        error:nil];
    timeArray=[fileString componentsSeparatedByString:@"\n"];

}
Mo Asif
  • 25
  • 5

1 Answers1

0

If your audioFilePath variable is coming back as nil after running pathForResource then the file simply couldn't be found in your bundle.

And since you've checked the value string to make sure it's correct and you know the stringWithFormat call is working you're left with the most likely explanation - that the files really don't exist in your bundle.

How have you added these MP3 files to your app? Are you sure they are part of the application target (if they're not they won't be put into the bundle)?

lxt
  • 31,146
  • 5
  • 78
  • 83