Search code examples
objective-ccocoaqtkit

How to display current time of a video?


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)


Solution

  • 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];
    }