In android, I am trying to implement an application that can play videos. In this application, I want to fire some callback functions for specific time periods of the video. For example when player plays 30th second of the video, I want to call a function f or when 1.30th second is playing function g is called.
How can I implement this functionality? I can start a timer for each function but in this case I have to track playbacks/forwards and modify timer according to these changes.. Do you have any idea?
I’ve never done this so I can’t give much information but just an idea…
Assuming you’re going to be using MediaPlayer then you could try extending it for a class of your own.
Within your own class you could maintain a
Handlerwhich usespostDelayed(Runnable r, long delayMillis)to trigger some code every 100ms, for example, and have it call thegetCurrentPosition()method. You could then have it check the current position against a list of ‘trigger points’ (30 seconds, 1 min 30 seconds etc) and then perform whatever task necessary.Doing it this way would mean if the play position is adjusted (using a seek bar, FF/RW etc) you wouldn’t need to update a timer – the
Runnablewould simply continue checking current position without it having to ‘know’ about any seek changes.