Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate MIDIKitSync into videoplayer app #133

Open
9SL9 opened this issue May 23, 2022 · 6 comments
Open

Incorporate MIDIKitSync into videoplayer app #133

9SL9 opened this issue May 23, 2022 · 6 comments

Comments

@9SL9
Copy link

9SL9 commented May 23, 2022

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)
}
@orchetect
Copy link
Owner

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.

@9SL9
Copy link
Author

9SL9 commented Jun 8, 2022

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

@9SL9 9SL9 closed this as completed Jun 8, 2022
@9SL9
Copy link
Author

9SL9 commented Jun 8, 2022

Accidentally closed. Sorry

@9SL9 9SL9 reopened this Jun 8, 2022
@orchetect
Copy link
Owner

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.

@orchetect orchetect transferred this issue from another repository Aug 30, 2022
@orchetect
Copy link
Owner

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.

@orchetect
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants