Search code examples
macoscocoamacos-high-sierranstouchbarmacbookpro-touch-bar

How to implement QuickTime-like video seeker/scrubber for Touch Bar in macOS?


I'm developing a video app and I want to implement a video seeker/scrubber just like in QuickTime where it displays small thumbs and I can seek by sliding my finger on the Touch Bar smoothly where it also displays the current progress in the video. I've seen NSScrubber class but it just displays a horizontal list of items. I can generate assets for specific times in video and display them but I need the exact functionality that QuickTime has. Is that bar something built in, or is it completely custom that I should build from scratch?


Solution

  • I was approaching it the wrong way. I've had an AVPlayer that I was controlling myself with its layer added manually into the hierarchy. I've switched to using AVPlayerView, which provided the seek/scrub functionality out of the box. The only culprit was that it was replacing the entire touch bar (where I had some other buttons too).

    I've overridden view controller's makeTouchBar method (every resource pointed out to using window controller's method instead of view controller's, which, in my case, wasn't working and the method wasn't even called on window controller even though the touch bar was tied to window controller, but it was called and worked perfectly on view controller) and added an other items proxy:

    -(NSTouchBar *)makeTouchBar{
        NSTouchBar *bar = self.view.window.windowController.touchBar;
        bar.defaultItemIdentifiers = [bar.defaultItemIdentifiers.mutableCopy arrayByAddingObject:@"NSTouchBarItemIdentifierOtherItemsProxy"];
        return bar;
    }
    

    And voila! It works perfectly now, with both my custom touch bar buttons and video player's seek/scrub bar.