Java Sound File getAudioDuration(final URI wavFile)

Here you can find the source of getAudioDuration(final URI wavFile)

Description

get Audio Duration

License

Open Source License

Declaration

public static double getAudioDuration(final URI wavFile)
            throws MalformedURLException, UnsupportedAudioFileException, IOException 

Method Source Code


//package com.java2s;
/*//from   ww  w .j a  va  2  s  .  c o  m
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Main {
    public static double getAudioDuration(final URI wavFile)
            throws MalformedURLException, UnsupportedAudioFileException, IOException {
        return getAudioDuration(new File(wavFile));
    }

    public static double getAudioDuration(final File wavFile)
            throws MalformedURLException, UnsupportedAudioFileException, IOException {
        AudioInputStream audio = null;
        try {
            audio = AudioSystem.getAudioInputStream(wavFile);
            final AudioFormat format = audio.getFormat();
            return wavFile.length() / format.getSampleRate() / (format.getSampleSizeInBits() / 8.0)
                    / format.getChannels();
        } finally {
            audio.close();
        }
    }
}

Related

  1. durationMillis(String audioPath)
  2. sliceWav(InputStream is, long starttime, long duration)
  3. getAudioDuration(final URI wavFile)