A movie player typically reads one chunk at a time from the movie file. I would use 2 threads to handle the video, with pseudocode like this:
Decode thread:
do
read next chunk (a chunk contains one or more frames)
decode chunk
add decoded frames to frame queue
while #frames in queue > safety margin
sleep((#frames in queue - safety margin)/framerate)
until EOF
End of movie
Display thread:
do
get next frame from frame queue
got frame?
if yes
display frame
if no
End of movie?
return
else
display the previous frame
sleep until it is time to display the next frame
until
Edit (in response to OP:s questions in comment):
how to set the safety margin?
The purpose of the safety margin is to avoid the picture from freezing if the computer has a load spike (decoding uses a lot of processing power). If the movie has 25 frames per second, setting the safety margin at 25 frames would keep a 1 second buffer of decoded frames. I would set it at some value, perhaps 1/3 of a second and then change it based on user feedback. It has to be larger than the time it takes to decode a chunk, otherwise the movie will start to freeze up, so the chunk size is a factor when deciding on the safety margin.
And when we pause video and open another video, of say 5 GB how memory swapping happening?
When we pause the video, the decoder thread will stop decoding (note that I changed the if
to a while
around the sleep).
When we start a new video the decoder thread closes the old movie file, empties the frame buffer and then starts decoding the new file. Programmatically I would probably let the old decoder thread die and start a new decoder thread for the new movie file.