henchman21
henchman21

Reputation: 13

Measure framerate for video playback

I'm new in Android and want to write an App that measure the framerate while playing video. For this purpose I use the MediaPlayer and show it on the SurfaceView. In this constellation, is it even possible to get the needed render statistics to calculate the Framerate? Thanks

Upvotes: 0

Views: 2404

Answers (2)

fadden
fadden

Reputation: 52313

The point of MediaPlayer is to conceal the details of video decoding and presentation, so you can't get at all the details. For example, there is no per-frame callback.

One way to approach the problem would be to play the video with MediaPlayer onto a SurfaceTexture, and render each frame onto the SurfaceView's Surface with OpenGL ES. The presentation time stamp of each frame is available from the SurfaceTexture.

Another approach is to use MediaCodec instead of MediaPlayer, giving you full control over the decoding and presentation process. This is more complicated, and gets tricky if the video has audio.

You may also want to consider examining the video file itself with MediaExtractor, and use the first few frames to detect the frame rate.

As noted by @Mick, some videos have a variable frame rate, and you cannot establish a single value for the entire stream. Video captured by screenrecord is a prime example. In such a case, the best you can do is show the "recent" frame rate, and you have to compute it dynamically (so MediaExtractor won't cut it).

Upvotes: 2

Mick
Mick

Reputation: 25471

It has been reported (I have not tested myself) that the VideoView's onDraw method will be called when every video frame is written.

This allows you to override and add some code to the onDraw method to note the time each frame is written and to calculate a running fps value.

To allow the onDraw method to be called you have to set a flag:

setWillNotDraw(false)

in your view constructor - see: https://stackoverflow.com/a/17595671/334402

BTW, it is worth being aware that some video will have a variable frame rate. This is usually because they are trying to maximise quality within a given bandwidth budget.

Upvotes: -1

Related Questions