所以,我正在完成一个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...
这只是永远重复,我不认为你们中任何一个可爱的人都能理解可能是什么样的人?
干杯!
答案 0 :(得分: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]; }