Incorporate MIDIKitSync into videoplayer app
9SL9 opened this issue · 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)
}
If you didn’t find a solution I may have time this week to develop an example project. If you got it working and want to share your code please do and I can adapt it into an example project.
I'll do some digging over the next few days to see how far I got and share what I have. There's some code here that may be useful (this is my question).
https://stackoverflow.com/questions/72342688/syncing-avplayer-play-start-time-with-timecode
Accidentally closed. Sorry
I've worked on applications that sync AVFoundation video using MIDIKitSync so I know it's possible. I might be able to check the implementation when I have time and get a minimum viable example working.
AVFoundation in general isn't perfect and there's some potential quirks but it does work.
Late response, I know - but I put some time into building a sample project. It's not done yet as it's taken me on numerous tangents - all good, and producing related new features in MIDIKit and TimecodeKit. But this is still on my list to get done. No ETA yet unfortunately.
Update: I've built a test app that can locate an AVPlayer to a specific timecode, and also when playback position changes (either by the user scrubbing the timeline or by playing the video), it will report back the current timecode as it changes.
Still getting to schedule video playback in the future but that's basically the next piece. Hopefully I can get time to hash that out soon.