2012-01-25 58 views
1

我想播放音頻文件,但希望一個接一個地播放它們。我看到了一些東西,但它並不真正起作用。例如,我想要那個聲音繼續說道,「肯」將「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); 
} 

回答

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 
    } 
} 
+0

我很抱歉,我對此非常感興趣。我不明白我如何將代碼與我的代碼示例相關聯。 你能舉個例子來說明我的例子嗎? 非常感謝 –

相關問題