2011-05-15 114 views
2

如何顯示視頻的當前時間?我在Obj-C開發,我正在使用QTKit框架。我想知道如何爲QTMovieView做不斷的通知我的函數「refreshCurrentTimeTextField」。我發現了一個蘋果樣本,但它很難提取我真正需要的東西。 (http://developer.apple.com/library/mac/#samplecode/QTKitMovieShuffler/Introduction/Intro.html)如何顯示視頻的當前時間?

回答

2

創建一個NSTimer是每秒更新一次:

[NSTimer scheduledTimerWithInterval:1.0 target:self selector:@selector(refreshCurrentTimeTextField) userInfo:nil repeats:YES]

然後創建你的定時器回調函數,並將時間轉換爲合適的HH:MM:SS標籤:

-(void)refreshCurrentTimeTextField { 
    NSTimeInterval currentTime; 
    QTMovie *movie = [movieView movie]; 
    QTGetTimeInterval([movie currentTime], &currentTime); 

    int hours = currentTime/3600; 
    int minutes = (currentTime/60) % 60; 
    int seconds = currentTime % 60; 

    NSString *timeLabel; 
    if(hours > 0) { 
     timeLabel = [NSString stringWithFormat:@"%02i:%02i:%02i", hours, minutes, seconds]; 
    } else { 
     timeLabel = [NSString stringWithFormat:@"%02i:%02i", minutes, seconds]; 
    } 
    [yourTextField setStringValue:timeLabel]; 
} 
+0

我猶豫是否使用NSTimer,但我後來認爲它是'錯誤的代碼'。但最後,根據你的答案和其他人不是。謝謝。也就是說,我仍然對QTKit感興趣......(如果可能......) – jlink 2011-05-16 04:52:02

+0

當模數處理double值時,int casts不見了。 int minutes =(int)(currentTime/60)%60; int seconds =(int)currentTime%60; – jlink 2011-05-16 20:46:23

相關問題