javaaudioformataudioinputstream

Retrieving the title, artist/author and duration of a .wav file


I am trying to get the properties of a .wav file I am using in my Java project using the following code. However when I run this code the methods format.getProperty("title"), format.getProperty("author"), and format.getProperty("duration") all return null. Should I be getting these details in a different way?

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(wavFile);
AudioFormat format = audioInputStream.getFormat();
Object[] temp = {false,
        format.getProperty("title"),
        format.getProperty("author"),
        format.getProperty("duration")};

Solution

  • WAV files are hard to work with, even though they can hold several metadata types. Depending upon the software and encoder used (16/32-bit PCM) you can have different configurations for the file and the Java Sound API would face problems to read this metadata.

    I tried a lot of tools and libs focused in audio metatagging (such as Apache Tika, jd3lib, mp3agic, Beaglebuddy, etc.) and most of them work fine with MP3 or other audio formats, but not for WAV files.

    Apparently, the better one for handling this is Jaudiotagger. It's simple, fast and you can accomplish your goal doing something like:

    AudioFile f = AudioFileIO.read(wavFile);
    WavTag tag = (WavTag) f.getTag();
    
    Object[] temp = {false,
            tag.getFirst(FieldKey.TITLE),
            tag.getFirst(FieldKey.ARTIST),
            f.getAudioHeader().getTrackLength() // In seconds
    };
    

    Also, refer the following links:

    Note: The support for this feature was added from version 2.2.4 above. So, always prefer the latest version to evict compatibility problems.