2011-05-15 113 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

相关问题