Returns the AudioFormat of an audio file - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

Returns the AudioFormat of an audio file

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.UnsupportedAudioFileException;

public class Main {
    /**// ww  w.ja  v a2s.  c  om
     * Returns the AudioFormat of an audio file
     * 
     * @param fileName
     *            the audio file
     * @return its audio format
     * @throws UnsupportedAudioFileException
     *             if there is a problem with audio format of the file not being
     *             supported by Java
     * @throws IOException
     *             if there is a problem reading from the file
     * @throws FileNotFoundException
     *             if the file does not exist or cannot be opened
     */
    public static AudioFormat audioFileFormat(String fileName)
            throws UnsupportedAudioFileException, IOException,
            FileNotFoundException {
        if (!new File(fileName).isFile()) {
            throw new FileNotFoundException("Error the audio file: "
                    + fileName + " does not exist");
        }

        AudioInputStream audioStream = AudioSystem
                .getAudioInputStream(new File(fileName));

        AudioFormat format = audioStream.getFormat();
        audioStream.close();
        return format;
    }
}

Related Tutorials