/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare 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 Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.gui.util;

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

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MakeSound {

    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    /**
     * @param filename the name of the file that is going to be played
     */
    public void playSound(String filename){

        String strFilename = filename;

        try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        sourceLine.start();

        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) {
                @SuppressWarnings("unused")
                int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
        sourceLine.close();
    }
}

@@ -776,17 +776,16 @@
 eption;%0A
-%0A
 import j
@@ -807,237 +807,9 @@
 led.
-AudioFormat;%0Aimport javax.sound.sampled.AudioInputStream;%0Aimport javax.sound.sampled.AudioSystem;%0Aimport javax.sound.sampled.DataLine;%0Aimport javax.sound.sampled.LineUnavailableException;%0Aimport javax.sound.sampled.SourceDataLine
+*
 ;%0A%0Ap
@@ -1034,16 +1034,8 @@
   /*
-*%0A     *
  @pa
@@ -1094,21 +1094,16 @@
 e played
-%0A    
  */%0A    
@@ -1313,36 +1313,8 @@
 ();%0A
-            System.exit(1);%0A
     
@@ -1472,36 +1472,8 @@
 ();%0A
-            System.exit(1);%0A
     
@@ -1819,36 +1819,8 @@
 ();%0A
-            System.exit(1);%0A
     
@@ -1839,32 +1839,32 @@
 (Exception e) %7B%0A
+
             e.pr
@@ -1884,36 +1884,8 @@
 ();%0A
-            System.exit(1);%0A
     

Remove system exits