play & sync a midi file to external clock

In principle it could work. I am guessing that if you just play when you get the event it will not work.

I would start by getting a copy of the MIDI standard and reading up on the messages you expect to process. Then capture what is coming down the line and check that it is as you expect. The code is then easy. The whole thing is straightforward but you need to get you head around it.