load Audio Resource - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

load Audio Resource

Demo Code


import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

public class Main{
    public static void main(String[] argv) throws Exception{
        String resourcePath = "java2s.com";
        System.out.println(loadAudioResource(resourcePath));
    }/*w ww  . j av a  2  s.  co m*/
    public static Clip loadAudioResource(String resourcePath)
            throws Exception {
        return loadAudio(FileHelper.getResourcePath(resourcePath));
    }
    public static Clip loadAudio(String path) throws Exception {
        Clip clip = null;

        AudioInputStream stream = AudioSystem.getAudioInputStream(new File(
                path));
        AudioFormat streamFormat = stream.getFormat();
        clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class,
                streamFormat));
        clip.open(stream);
        return clip;
    }
}

Related Tutorials