一个接一个播放音频文件?

时间:2012-01-25 22:10:44

标签: iphone ios xcode ipad ios5

我想播放音频文件,但想一个接一个播放。我看到了一些东西,但它确实不起作用。例如,我希望声音继续,“ken”将在“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);
 }   

1 个答案:

答案 0 :(得分:2)

你必须低于系统声音。如果它很简单,您可以使用AVAudioPlayer并在每次获得声音完成的委托回调时触发下一个声音。像

这样的东西
#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
    }
}