2

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.

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83

3 Answers3

8

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
                }
            }
        }
    }
}
mrj
  • 589
  • 1
  • 7
  • 17
  • thanks for sharing much appreciated, do you have java version of it? – DIRTY DAVE Feb 08 '20 at 21:27
  • 1
    No sorry I'm writing in full Kotlin. However, it's easy enough to convert. Google anything you don't understand, although Kotlin lets itself be read like english :) – mrj Feb 08 '20 at 22:33
  • I am stream a HSL video which has 2 audio and need to switch on user action, so I guess I need to first keep audio information in a data structure and then on user action listner I can set language on trackselector...can you guide me here? – Vikas Pandey Oct 27 '21 at 14:15
7

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.

Hamza Khan
  • 1,433
  • 13
  • 19
  • Strange. When I loop through the track groups, I can get a Format object (not type), which in turn doesn't contain any audio parameter. Could you post a code example? – mrj Feb 08 '20 at 12:30
  • How do i know the audio track information in advance – Vikas Pandey Oct 27 '21 at 14:15
2
 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!

Timur Ugurlu
  • 183
  • 1
  • 2
  • 13