MIDIKit icon indicating copy to clipboard operation
MIDIKit copied to clipboard

Incorporate MIDIKitSync into videoplayer app

Open 9SL9 opened this issue 2 years ago • 6 comments

Thanks a lot for sharing your great work.

I'm hoping to use MIDIKitSync to syncronise playback of an avplayer in my app with an external DAW. I'm planning on using the following code to schedule video playback in the future (e.g. I want video play back to occur at a particular future timecode). I'm a bit unsure how to sync with MTC however. I'm guessing I need to ensure they're both using the same sourceClock. Any ideas how to do this? Sorry, if this is already taken care of, still learning. Thanks again.

var audioClock = CMClockGetHostTimeClock()
let videoPlayer = AVPlayer(url: videoURL)
videoPlayer.sourceClock = audioClock
videoPlayer.automaticallyWaitsToMinimizeStalling = false

func schedulePlayback(videoTime: TimeInterval, hostTime: UInt64) {
    videoPlay(at: 0, hostTime: hostTime)
}

func videoPlay(at time: TimeInterval = 0, hostTime: UInt64 = 0) {
    let cmHostTime = CMClockMakeHostTimeFromSystemUnits(hostTime)
    let cmVTime = CMTimeMakeWithSeconds(time, preferredTimescale: 1000000)
    let futureTime = CMTimeAdd(cmHostTime, cmVTime)
    videoPlayer.setRate(1, time: kCMTimeInvalid, atHostTime: futureTime)
}

9SL9 avatar May 23 '22 07:05 9SL9