I don't think you need a whole new if block for this.

Some ideas:

You can make hidden a variable that you set to true or false, which you then pass into the same callback.

Also make the audio/video element a generic named variable, then set it to an audio or video element, which both should have the same APIs.

I also think you can move the image check to be first, and return early. Then make the audio and video check be a catch all of sorts.

Something along those lines, but I think this can be boiled down more.