Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.BufferedInputStream;
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.DataLine;
import javax.sound.sampled.SourceDataLine;

public class Main {
    private final int BUFFER_SIZE = 128000;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;
    private boolean running = false;

    public void playSound(final String filename) throws Exception {
        InputStream audioSrc = this.getClass().getResourceAsStream(filename);

        InputStream bufferedIn = new BufferedInputStream(audioSrc);
        audioStream = AudioSystem.getAudioInputStream(bufferedIn);

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceLine.open(audioFormat);

        sourceLine.start();
        running = true;

        while (running) {
            audioStream.mark(BUFFER_SIZE);
            int nBytesRead = 0;
            byte[] abData = new byte[BUFFER_SIZE];
            while (nBytesRead != -1) {
                try {
                    nBytesRead = audioStream.read(abData, 0, abData.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0) {
                    sourceLine.write(abData, 0, nBytesRead);
                }
            }
            if (running)
                audioStream.reset();
        }
        sourceLine.drain();
        sourceLine.close();
    }

    public void stopSound() {
        this.running = false;
        if (sourceLine != null) {
            sourceLine.stop();
            sourceLine.drain();
            sourceLine.close();
        }
    }
}