package org.snitko.app.playback;


/**
 * Copyright [2015] vadim
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p>
 * Created: 2/1/16
 */

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.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;


public class PlaySoundMP3 {
    public void play(String filePath) {
        final File file = new File(filePath);

        try (final AudioInputStream in = getAudioInputStream(file)) {

            final AudioFormat outFormat = getOutFormat(in.getFormat());
            final Info info = new Info(SourceDataLine.class, outFormat);

            try (final SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info)) {

                if (sourceDataLine != null) {
                    sourceDataLine.open(outFormat);
                    sourceDataLine.start();
                    AudioInputStream audioInputStream = getAudioInputStream(outFormat, in);
                    stream(audioInputStream, sourceDataLine);
                    sourceDataLine.drain();
                    sourceDataLine.stop();
                }
            }

        } catch (UnsupportedAudioFileException
                | LineUnavailableException
                | IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
    }

    private void stream(AudioInputStream in, SourceDataLine sourceDataLine) throws IOException {
        final byte[] buffer = new byte[4096];
        for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
            sourceDataLine.write(buffer, 0, n);
        }
    }

    public static void main(String[] args) {
        final PlaySoundMP3 player = new PlaySoundMP3();
        player.play("/Users/vadim/output1452519755900.mp3");
    }

}

@@ -2845,61 +2845,146 @@
     
-player.play(%22/Users/vadim/output1452519755900.mp3%22
+if (args.length != 1) %7B%0A            args = new String%5B%5D%7B%22/Users/vadim/output1452519755900.mp3%22%7D;%0A        %7D%0A        player.play(args%5B0%5D
 );%0A
+%0A
     

allow for mp3 file name passing in