shay cohen
shay cohen

Reputation: 11

Play audio files one after another?

I want to play audio files, but would like to play them one after another. I saw some things, but it does not really work. For example, I want that the sound continues, "ken" will play after the "lo":

 -(IBAction)LO:(id)sender{


    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;

    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"LO", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

    }

-(IBAction)KEN:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"KEN", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
 }   

Upvotes: 1

Views: 1244

Answers (1)

Art Gillespie
Art Gillespie

Reputation: 8757

You'll have to go lower-level than system sounds. If it's simple, you can use AVAudioPlayer and fire off the next sound each time you get the delegate callback that the sound has finished. Something like

#pragma mark - AVAudioPlayerDelegate

– (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)success {
    if (success) {
        // last sound finished successfully, play the next sound
        [self playNextSound];
    } else {
        // something went wrong, show an error?
    }
}

/*
 * Play the next sound
 */
- (void)playNextSound {
    // get the file location of the next sound using whatever logic
    // is appropriate
    NSURL *soundURL = getNextSoundURL();
    NSError *error = nil;
    // declare an AVAudioPlayer property on your class
    self.audioPlayer = [[AVAudioPlayer alloc] initWithURL:soundURL error:&error];
    if (nil != error) {
        // something went wrong... handle the error
        return;
    }
    self.audioPlayer.delegate = self;
    // start the audio playing back
    if(![self.audioPlayer play]) {
        // something went wrong... handle the error
    }
}

Upvotes: 3

Related Questions