2012-05-25 2 views
2

나는 아이폰을 처음 사용합니다. 나는 오디오 플레이어에서 일하고있다. 오디오 클립에 노래의 현재 시간과 남은 시간을 표시해야합니다. 비디오 플레이어에서는 기본적으로 가져 오지만 audioplayer에서는 음악의 현재 시간을 가져 오기위한 논리를 작성하지 않습니다. 아래의 코드는 여기 audioPlayer이 AVAudioPlayer의 인스턴스이며 startDurationLabel이 노래의 현재 시간 표시 라벨입니다AVAudioPlayer에서 재생되는 노래의 남은 시간을 얻는 방법

int minutes = (int)audioPlayer.currentTime/60; 
int seconds = (int)audioPlayer.currentTime % 60; 
startDurationLabel.text = [NSString stringWithFormat:@"%d:%02d",minutes,seconds]; 

입니다.

그러나 나는이 일을하고 어떤 몸이 좀 도와 줘요 알고 있다면 노래

의 남은 시간을 표시하려면이 논리를 얻기 위해 고군분투 ...

답변

2
NSTimeInterval remaining = audioPlayer.duration - audioPlayer.currentTime; 
+2

나는 내 자신의 게시물 외에 아무 것도 언급 할만큼 충분한 평판이 없으므로 여기에 의견을 남겨 두겠습니다. @Ayaz가 GCFloat를 사용하는 것은 의미가 있지만, 의미 상 올바르지는 않습니다. CGFloat는 Core Graphics에서 사용되며 그래픽 속성이 아닙니다. 후드에서 NSTimeInterval은 double로 typedef되고 CGFloat는 float로 사용됩니다. 따라서 빼기 반환 값을 캐스팅하면 정밀도가 떨어집니다 (초를 표시 할 때만 중요 함). –

2

CGFloat remainingTime = audioPlayer.duration - audioPlayer.currentTime 
시도
0

이 시도 -

NSString *strTimeLeft = [self getTimeFromTimeInterval:CMTimeGetSeconds(_player.currentItem.duration) - CMTimeGetSeconds(_player.currentTime)]; 

클래스에이 메서드 추가

- (NSString*)getTimeFromTimeInterval:(NSTimeInterval)timeInterval 
{ 
    NSInteger interval = (NSInteger)timeInterval; 
    NSInteger seconds = interval%60; 
    NSInteger minutes = (interval/ 60)%60; 
    //NSInteger hr = (interval/3600)%60; 

    NSString *strTime = [NSString stringWithFormat:@"%02d:%02d",minutes,seconds]; 
    return strTime; 
} 
관련 문제