read Music Audio File and return Clip - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

read Music Audio File and return Clip

Demo Code

/*******************************************************************************
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
import java.io.IOException;
import java.io.InputStream;
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;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Main{
    public static Clip readMusicFile(InputStream audioFilePath,
            SoundOutput soundOutput) throws UnsupportedAudioFileException,
            IOException, LineUnavailableException {
        AudioInputStream audioStream = AudioSystem
                .getAudioInputStream(audioFilePath);
        AudioFormat format = audioStream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);

        Mixer mixer = soundOutput.getMixer();
        Clip audioClip = (Clip) mixer.getLine(info);
        audioClip.open(audioStream);/*from  w w  w . ja va  2 s  .co m*/
        return audioClip;
    }
}

Related Tutorials