簡體   English   中英

Objective-C / iPhone中的音頻循環

[英]Audio looping in Objective-C/iPhone

因此,我正在完成一個iPhone App。

我具有以下代碼來播放文件:

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...

這只是永遠的重復,我不認為你們中任何一個可愛的人都能理解可能是什么?

干杯!

我不確定您的代碼有什么問題,可能是[player play]是一個異步調用,但是您永遠循環播放,而沒有讓Player真正開始玩或意識到它實際上在玩。 我不建議在任何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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM