0
votes

I am trying to play back 4 audio files at random when they are called. here is code

  // randomize the playback on the setShot files
   int randomNumber = arc4random() % 4 + 1;
   NSString *tmpFileName = [[NSString alloc] initWithFormat:@"SetShot%d", randomNumber];
   NSString *fileName = [[NSBundle mainBundle] pathForResource:tmpFileName ofType:@"aif"];
   SystemSoundID soundID;
   AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:fileName], &soundID);
   AudioServicesPlaySystemSound (soundID);

I am getting the above crash on my device not in the simulator. I am fairly new to coding. Thanks for all your help.

The sound files are SetShot01 thru to SetShot04

ARC-armv6,armv7 xcode 4.3.2 on device with 5.1.1

1
Try logging fileName, seems like this is coming nil.rishi
You're ab- and misusing printf-style functions. If you do so, at least know how they work: kernel.org/doc/man-pages/online/pages/man3/sprintf.3.htmluser529758

1 Answers

2
votes

Your tmpFileName file probably doesn't exist in the main bundle. That would result in a nil URL. This is because you said that your file names are SetShot0[1-4] whereas the strings you are creating are SetShot[1-4]. Change

NSString *tmpFileName = [[NSString alloc] initWithFormat:@"SetShot%d", randomNumber];

to read as

 NSString *tmpFileName = [[NSString alloc] initWithFormat:@"SetShot0%d", randomNumber];