2012-03-01 2 views
4

AVAudioPlayer's 현재 재생 시점을 확인하는 것이 올바른 방법입니까?avaudioplayer의 현재 재생 시점 확인

[audioPlayer play]; 

float seconds = audioPlayer.currentTime; 

float seconds = CMTimeGetSeconds(duration); 

currentTime을 다음 11초

[self performSelector:@selector(firstview) withObject:nil]; 

및 firstview 후 동일한 경우

[audioPlayer play]; 

currentTime을가 23초

[self performSelector:@selector(secondview) withObject:nil]; 
동일 할 때 이후 난 코드 어떻게

Th 당신의 대답을 기다리고 있습니다.

+0

시간을 확인하는 병렬 타이머를 예약하십시오. 'currentTime'는 NSTimeInterval 변수를 반환하므로'double seconds = audioPlayer.currentTime; '이 올바른 것입니다. – 0xDE4E15B

답변

7

정기적으로 시간을 확인하기 위해 NSTimer을 설정할 수 있습니다.

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
    target:self 
    selector:@selector(checkPlaybackTime:) 
    userInfo:nil 
    repeats:YES]; 

은 자세한 내용은 NSTimer 설명서를 참조하십시오 :

- (void) checkPlaybackTime:(NSTimer *)theTimer 
{ 
    float seconds = audioPlayer.currentTime; 
    if (seconds => 10.5 && seconds < 11.5) { 
    // do something 
    } else if (seconds >= 22.5 && seconds < 23.5) { 
    // do something else 
    } 
} 

그런 다음 모든 초 (당신이 좋아하는 또는 어떤 간격)이 메소드를 호출하기 위해 NSTimer 객체를 설정 : 당신은 같은 방법을해야합니다.

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

편집 : 당신이 그것으로 통해있을 때 당신이 바로 그 시간에 타이머를 정지 (무효)해야합니까 seconds 아마 정확히 11 또는 23되지 않습니다; 세분성을 가지고 바이올린을해야합니다.

+0

1 초 경계 울타리가 마음에 들지 않습니다. 그들은 신뢰할 수없는 것처럼 보입니다. 'if (seconds => 11) {.. performSelector :라고 말하면, 이미 일어난 것을 나타내는 플래그를 설정하십시오. }' – bobobobo

관련 문제