2

How to do to display current time of a video ? I am developing in Obj-C and I am using QTKit framework. I am wondering how to do for that QTMovieView notify continually my function "refreshCurrentTimeTextField". I found an Apple sample but it turned to be quite difficult to extract what I really need. (http://developer.apple.com/library/mac/#samplecode/QTKitMovieShuffler/Introduction/Intro.html)

jlink
  • 682
  • 7
  • 24

1 Answers1

2

Create an NSTimer that updates once every second:

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

And then create your timer callback function and turn the time into a proper HH:MM:SS label:

-(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];
}
iii
  • 1,594
  • 1
  • 9
  • 8
  • I hesitated to use NSTimer but I then thought it was 'bad code' . But finally, according your answers and others people it is not. Thanks. That said, i'm still interested by doing it with QTKit... (if it is possible...) – jlink May 16 '11 at 04:52
  • int casts were missing when modulo were dealing with double value. int minutes = (int)(currentTime/60) % 60; int seconds = (int)currentTime % 60; – jlink May 16 '11 at 20:46