1234567
1234567

Reputation: 2485

How can we find if a video contains Audio or not in Android

How can we find if a video contains Audio or not in Android

Is there any way in Android to find if a video contains audio

Upvotes: 5

Views: 2227

Answers (3)

ohlab
ohlab

Reputation: 467

more simple, null pointer exception safe

boolean hasAudio = "yes".equals(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO));

Upvotes: 1

King of Masses
King of Masses

Reputation: 18775

Yes. It is possible. You need to look into the MediaMetadataRetriever

By using METADATA_KEY_HAS_AUDIO you can check whether the video has the audio or not.

private boolean isVideoHaveAudioTrack(String path) {
        boolean audioTrack =false;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(path);
        String hasAudioStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
        if(hasAudioStr!=null && hasAudioStr.equals("yes")){ 
         audioTrack=true; } 
        else{ 
        audioTrack=false; }

        return audioTrack;
    }

Here path is your video file path.

Upvotes: 11

ClassA
ClassA

Reputation: 2680

Just to add to @KingofMasses answer, if the file doesn't have a audio file then String hasAudioStr will return null. The string will then through a nullpointerexception.

here is how I resolved this:

private boolean doesVideoHaveAudioTrack(String path) {
    boolean audioTrack;

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(path);
    String hasAudioStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
    if (hasAudioStr==null){
        audioTrack = false;
    }else {
        audioTrack = true;
    }

    return audioTrack;
}

All credit goes to @KingofMasses, I just wanted to point out the issue.

Upvotes: 5

Related Questions