2010-03-19 31 views
1

因此,我正在完成一个iPhone应用程序。Objective-C/iPhone中的音频循环

我已经制定了下面的代码来播放该文件:

while(![player isPlaying]) { 
    totalSoundDuration = soundDuration + 0.5; //Gives a half second break between sounds 
    sleep(totalSoundDuration); //Don't play next sound until the previous sound has finished 
    [player play]; //Play sound 
    NSLog(@" \n Sound Finished Playing \n"); //Output to console 
} 

出于某种原因,声音播放一次,然后代码回路,它输出以下内容:

Sound Finished Playing 
Sound Finished Playing 
Sound Finished Playing 
etc... 

这只是我永远不会重复,我想你们中任何一个可爱的人都无法理解这可能是什么?

干杯!

回答

3

我不完全确定你的代码有什么问题,它可能是[播放器播放]是一个异步调用,但你永远循环而不让播放器真正开始播放或意识到它实际上在播放。我不建议在任何iPhone应用程序中使用sleep,因为整个模型基于异步事件。

我没有测试这个代码,甚至没有编译它,但我希望你能从中得到这个想法。

 
- (void) startPlayingAgain:(NSTimer *)timer 
{ 
    AVAudioPlayer *player = timer.userInfo; 

    [player play]; 
} 

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 
         successfully:(BOOL)flag 
{ 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 
          target:self 
          selector:@selector(startPlayingAgain:) 
          userInfo:player 
          repeats:NO]; 
} 

- (void)startPlaying:(NSString *)url 
{ 
    AVAudioPlayer *player = [[AVAudioPlayer alloc] 
           initWithContentsOfURL:url error:NULL]; 

    player.delegate = self; 

    [player play]; 
} 

+0

你的意思是这样的: - (无效)audioPlayerDidFinishPlaying:(的NSString *)audioPlayer完成:(BOOL *)完成背景:(无效*)上下文{} - 你可以弹出了我一个简单的例子任何机会,请? :) – 2010-03-19 10:02:57

+0

当然,给我第二个... – 2010-03-19 10:09:01

+0

非常感谢! – 2010-03-19 10:41:45