package net.liquidpineapple.pang;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import net.liquidpineapple.pang.logger.Logger;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Audiosystem for the application. Plays the background music.
 *
 * @author Govert de Gans
 * @date 2016-09-22
 */

public class AudioSystem {

  private static Player player;
  private static String loopingSound = "";
  private static boolean isRunning;
  private static boolean playMusic = true;

  /**
   * Start the background music loop.
   */
  public static void start() {
    playMusic = true;
    new Thread(AudioSystem::playLoop).start();
  }

  private static void playLoop() {
    isRunning = true;
    byte[] buffer = null;
    try {
      String currentLoop = "";
      while (true) {
        if (!currentLoop.equals(loopingSound)) {
          BufferedInputStream resourceStream = new BufferedInputStream(
              Application.class.getResourceAsStream(loopingSound));
          buffer = new byte[resourceStream.available()];
          resourceStream.read(buffer);
          currentLoop = loopingSound;
        }

        if (buffer != null && playMusic) {
          Logger.info("Music loop is running");
          player = new Player(new ByteArrayInputStream(buffer));
          player.play();
        }
      }
    } catch (JavaLayerException | IOException ex) {
      Logger.error("The AudioSystem has crashed.", ex);
    }
    isRunning = false;
  }

  /**
   * Change the looping background music to another file.
   *
   * @param newLoopingSound {@link String} URL to the new looping sound.
   */
  public static void changeLoopingSound(String newLoopingSound) {
    if (!loopingSound.equals("") && isRunning && player != null) {
      player.close();
    }
    loopingSound = newLoopingSound;
  }

  /**
   * Stops the music from playing.
   */
  public static void stop() {
    playMusic = false;
    try {
      player.close();
    } catch (NullPointerException ex) {
      Logger.warning("Attempted to close player but was null");
    }
  }
}

@@ -109,16 +109,17 @@
 Player;%0A
+%0A
 import n
@@ -260,16 +260,199 @@
 ption;%0A%0A
+import javax.sound.sampled.Clip;%0Aimport javax.sound.sampled.LineEvent;%0Aimport javax.sound.sampled.LineUnavailableException;%0Aimport javax.sound.sampled.UnsupportedAudioFileException;%0A%0A
 /**%0A * A
@@ -2318,11 +2318,743 @@
    %7D%0A  %7D
+%0A%0A  public static void playEffect(String path) %7B%0A    try %7B%0A      Logger.info(%22Playing sound effect %22 + path);%0A      Clip clip = javax.sound.sampled.AudioSystem.getClip();%0A      clip.open(javax.sound.sampled.AudioSystem.getAudioInputStream(Application.class.getResourceAsStream(path)));%0A      clip.addLineListener((lineEvent) -%3E %7B%0A        if (lineEvent.getType() == LineEvent.Type.STOP)%0A          clip.close(); // close resources when clip is finished.%0A      %7D);%0A      clip.start();%0A    %7D catch (LineUnavailableException %7C IOException e) %7B%0A      Logger.error(%22Could not play sound effect%22, e);%0A    %7D catch (UnsupportedAudioFileException e) %7B%0A      Logger.error(%22The AudioSystem tried to play an unsupported audio file%22, e);%0A    %7D%0A  %7D
 %0A%7D%0A

Add function for playing sound effects