WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2731

Exoplayer - How to check if MP4 video has audio?

I'm using URLs from an API. Some of the URLs are mp4's without sound(video is playing just no sound). How do I check if that video has sound or not? I've been searching through SimpleExoPlayer docs and testing the methods on my URLS

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.html for the past couple hours

But I can't figure out how to detect check if the video playing has sound or not.

Tried all the methods in getAudioAttributes(), getAudioComponents() and now just tried getAudioFormat() but they all return null.

try{
     Log.d(TAG, "onCreateView: " + player.getAudioFormat().channelCount);
}catch (Exception e){
     Log.d(TAG, "onCreateView: " + e);
}

And yes I've made sure the link's actually have Audio.

Upvotes: 2

Views: 2907

Answers (3)

Timur Ugurlu
Timur Ugurlu

Reputation: 193

 player.addListener(new Player.EventListener() {
            @Override
            public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
                if (trackGroups != null && !trackGroups.isEmpty()) {
                    for (int i = 0; i < trackGroups.length; i++) {
                        for (int g = 0; g < trackGroups.get(i).length; g++) {
                            String sampleMimeType = trackGroups.get(i).getFormat(g).sampleMimeType;
                            if (sampleMimeType != null && sampleMimeType.contains("audio")) {
                                                    //video contains audio
                            }
                        }
                    }
                }
            }
        }

JAVA version of mrj answer

Came across this thread which helped me a lot!

Upvotes: 2

mrj
mrj

Reputation: 629

To complete @Hamza Khan's answer, here is my code to check whether the loaded video has any audio:

override fun onTracksChanged(
    trackGroups: TrackGroupArray?,
    trackSelections: TrackSelectionArray?
) {
    if (trackGroups != null && !trackGroups.isEmpty){
        for (arrayIndex in 0 until trackGroups.length){
            for (groupIndex in 0 until trackGroups[arrayIndex].length){
                val sampleMimeType = trackGroups[arrayIndex].getFormat(groupIndex).sampleMimeType
                if ( sampleMimeType != null && sampleMimeType.contains("audio") ){
                    //video contains audio
                }
            }
        }
    }
}

Upvotes: 8

Hamza Khan
Hamza Khan

Reputation: 1521

You can track the current tracks with Player#EventListener#OnTracksChanged and get the current ones with Player#getCurrentTrackGroups(). If you go through the track groups you can look for the type. If you find AUDIO type there that means your video file contains the audio track.

If you additionally want to check if any of the audio tracks was selected, then Player#getCurrentTrackSelections() is the place to look at.

Upvotes: 7

Related Questions