How to debug error -1500 returned by AudioServicesCreateSystemSoundID?

982 views Asked by At

Any help really appreciated. See my questions (1) and (2) at the bottom.

I'm new to Objective C programming, coming from Flex development. I've tried for the first time doing a simple sound playback. I'm failing, and this is a common problem when I look for answer on the Internet. None of the solutions I have found seem to work, however. I'm just trying to play a short .caf file through the following method:

- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
  NSLog(@"Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);

  SystemSoundID soundID = 0;
  NSString *filePath = [NSString stringWithFormat:@"%@.%@", file, extension];
  CFURLRef soundFileURL = (__bridge CFURLRef)[NSURL URLWithString:filePath];
  OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);

  NSLog(@"filePath: %@, soundFileURL: %@, errorCode: %ld", filePath, soundFileURL, errorCode);

  if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);

  AudioServicesPlaySystemSound(soundID);
}

I'm calling that as follows:

// Start with playing the audio effect for correct answer:
[self playUserInterfaceSoundEffect:@"answer_correct" ofType:@"caf"];

The sound doesn't play however. The attempt to create the system sound ID returned error code -1500, as can be seen in the log trace statements:

2013-09-10 17:50:48.498 InFocus-dev[25888:c07] Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf 
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] filePath: answer_correct.caf, soundFileURL: answer_correct.caf, errorCode: -1500 
2013-09-10 17:50:48.498 InFocus-dev[25888:c07] ERROR -1500: Failed to initialize UI audio file answer_correct.caf

Apparently that error code is a pretty generic one, as described here: (OSStatus) error = -1500 in AudioToolbox Framework -- it stands for kAudioServicesSystemSoundUnspecifiedError and may mean that the audio file is perhaps not in the right format or cannot be found.

My questions are:

(1) The audio files should be in the right format, but how can I make sure? This short post seems to suggest that some files can trigger this error, but it's not clear for what reason: http://www.dosomethinghere.com/2009/05/05/audioservicescreatesystemsoundid-error-1500/

(2) The application should be able to find the files, but how can I make sure? They are located in resources/assets/sounds/. Images that are located in resources/assets/icons/ are found without problems, for example. How can I test whether the application simply cannot find the files at all?

I feel like such a N00b -- thanks for any help!

Erik


I'm new to StackOverflow, so can't answer my own question yet, but here it is:

Okay, as @borrrden pointed out, the answer is to get the full file path using NSBundle.

Having rewritten the method as follows fixed the problem:

- (void) playUserInterfaceSoundEffect:(NSString *)file ofType:(NSString *)extension
{
  NSLog(@"Sound FX: Call to playerUserInterfaceSoundEffect:%@ ofType:%@", file, extension);

  NSString *filePath  = [[NSBundle mainBundle] pathForResource:file ofType:extension];
  NSURL *fileURL      = [NSURL fileURLWithPath:filePath];
  CFURLRef inFileURL  = (CFURLRef)CFBridgingRetain(fileURL);

  SystemSoundID soundID;
  OSStatus errorCode  = AudioServicesCreateSystemSoundID(inFileURL, &soundID);

  NSLog(@"\nfilePath:\t%@\nfileURL:\t%@\ninFileURL:\t%@", filePath, fileURL, inFileURL);

  if (errorCode != 0) NSLog(@"ERROR %ld: Failed to initialize UI audio file %@", errorCode, filePath);

  AudioServicesPlaySystemSound (soundID);
}

And this log trace now looks like this:

2013-09-10 19:58:56.165 InFocus-dev[27986:c07] Sound FX: Call to playerUserInterfaceSoundEffect:answer_correct ofType:caf  
2013-09-10 19:58:56.165 InFocus-dev[27986:c07] 
filePath:   /Users/erik/Library/Application Support/iPhone Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
fileURL:    file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf
inFileURL:  file://localhost/Users/erik/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/5A8B1CC8-2E32-4469-80C2-FA311242B5C5/InFocus-dev.app/answer_correct.caf

So relieved..!

Thanks, Erik

1

There are 1 answers

0
Alex On

Some issues I addressed to fix error -1500 in my project.

  1. Trim the audio clip to under 5 seconds. My 45 second audio file was apparently too long.
  2. Convert your file to Apple's Core Audio Format (.caf)
  3. Fill in the basic ID3 metadata tags

All 3 of these steps I completed with the free Audacity software.

When adding the audio file to your project, make sure the file is copied into your bundle's resources (Build Phases > Copy Bundle Resources).

Below is my working code for Swift 4.2 on iOS 12.1.2.

import AudioToolbox

var soundID: SystemSoundID = 0

guard let audioFileURL = Bundle.main.url(forResource: "your_audio_file", withExtension: "caf") else {
    print("Unable to find audio file.")
    return
}

AudioServicesCreateSystemSoundID(audioFileURL as CFURL, &soundID)

AudioServicesAddSystemSoundCompletion(soundID, nil, nil, { soundID, _ in
    AudioServicesDisposeSystemSoundID(soundID)
}, nil)

AudioServicesPlaySystemSound(soundID)