Shoaibi
Shoaibi

Reputation: 889

Using system Sound to play sounds

Here is the code:

-(void)stop
{
    NSLog(@"Disposing Sounds");
    AudioServicesDisposeSystemSoundID (soundID);
    //AudioServicesRemoveSystemSoundCompletion (soundID);
}

static void completionCallback (SystemSoundID  mySSID, void* myself) {
    NSLog(@"completion Callback");
}
- (void) playall: (id) sender {

    [self stop];

    AudioServicesAddSystemSoundCompletion (soundID,NULL,NULL,
     completionCallback,
     (void*) self);


    OSStatus err = kAudioServicesNoError;
    NSString *aiffPath = [[NSBundle mainBundle] pathForResource:@"slide1" ofType:@"m4a"];
    NSURL *aiffURL = [NSURL fileURLWithPath:aiffPath];
    err = AudioServicesCreateSystemSoundID((CFURLRef) aiffURL, &soundID);
    AudioServicesPlaySystemSound (soundID);
    NSLog(@"Done Playing");
}

Output:

Disposing Sounds
Done Playing

In actual no sound gets play at all and completion call back isn't called as well. Any idea what could be wrong here? I want to stop any previous sound before playing current.

Upvotes: 1

Views: 2730

Answers (3)

Govind
Govind

Reputation: 2348

You can use AudioToolBox framework for this:

CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(1100);

Upvotes: 0

Chris Gummer
Chris Gummer

Reputation: 4782

AFAIK, the only supported files are .caf, .aif, or .wav:

To play your own sounds, add the sound file to your application bundle; the sound file must adhere to the following requirements:

  • Must be .caf, .aif, or .wav files.
  • The audio data in the file must be in PCM or IMA/ADPCM (IMA4) format.
  • The file's audio duration must be less than 30 seconds.

Audio & Video Coding How-To's

Upvotes: 1

Dave Gamble
Dave Gamble

Reputation: 4174

What does err contain? From that you should be able to infer the problem.

Upvotes: 0

Related Questions