Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 <sigonasr2@gmail.com>main
parent
0d5ccae00b
commit
79caec1fdf
@ -0,0 +1,29 @@ |
|||||||
|
package sig.engine; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import javax.sound.sampled.Clip; |
||||||
|
import javax.sound.sampled.LineEvent; |
||||||
|
import javax.sound.sampled.LineListener; |
||||||
|
import javax.sound.sampled.LineEvent.Type; |
||||||
|
|
||||||
|
public class MusicPlayer implements LineListener{ |
||||||
|
public static SongThread SongThread; |
||||||
|
public static List<Clip> SoundEffects; |
||||||
|
public static boolean songChangeRequested=false; |
||||||
|
|
||||||
|
public static void init() { |
||||||
|
SongThread = new SongThread(); |
||||||
|
} |
||||||
|
public static void changeMusic(Song newSong) { |
||||||
|
SongThread.currentSong=newSong; |
||||||
|
MusicPlayer.songChangeRequested=true; |
||||||
|
SongThread.run(); |
||||||
|
} |
||||||
|
@Override |
||||||
|
public void update(LineEvent event) { |
||||||
|
if (event.getType()==Type.CLOSE||event.getType()==Type.STOP) { |
||||||
|
SongThread.run(); //Cause a loop so the music continues playing.
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
package sig.engine; |
||||||
|
|
||||||
|
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; |
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException; |
||||||
|
|
||||||
|
public enum Song{ |
||||||
|
WOOLOOKOLOGIE(new File("..","Woolookologie.wav")); |
||||||
|
|
||||||
|
File songFile; |
||||||
|
|
||||||
|
public Song(File f) { |
||||||
|
this.songFile=songFile; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
package sig.engine; |
||||||
|
|
||||||
|
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; |
||||||
|
import javax.sound.sampled.UnsupportedAudioFileException; |
||||||
|
|
||||||
|
import sig.RabiClone; |
||||||
|
|
||||||
|
public class SongThread extends Thread{ |
||||||
|
Song currentSong; |
||||||
|
AudioInputStream audioStream; |
||||||
|
AudioFormat format; |
||||||
|
DataLine.Info info; |
||||||
|
SourceDataLine audioLine; |
||||||
|
public void run() { |
||||||
|
try { |
||||||
|
if (audioStream!=null) { |
||||||
|
audioLine.close(); |
||||||
|
audioStream.close(); |
||||||
|
audioStream=null; |
||||||
|
} |
||||||
|
audioStream = AudioSystem.getAudioInputStream(audioStream); |
||||||
|
format = audioStream.getFormat(); |
||||||
|
info = new DataLine.Info(SourceDataLine.class,format); |
||||||
|
audioLine = (SourceDataLine)AudioSystem.getLine(info); |
||||||
|
audioLine.addLineListener(RabiClone.MUSICPLAYER); |
||||||
|
audioLine.open(format); |
||||||
|
audioLine.start(); |
||||||
|
final int BUFFER_SIZE=4096; |
||||||
|
byte[] bytesBuffer = new byte[BUFFER_SIZE]; |
||||||
|
int bytesRead=-1; |
||||||
|
while ((bytesRead=audioStream.read(bytesBuffer))!=-1&&!MusicPlayer.songChangeRequested) { |
||||||
|
audioLine.write(bytesBuffer,0,bytesRead); |
||||||
|
} |
||||||
|
if (!MusicPlayer.songChangeRequested) { |
||||||
|
audioLine.drain(); |
||||||
|
} else { |
||||||
|
audioLine.stop(); |
||||||
|
audioLine.flush(); |
||||||
|
} |
||||||
|
} catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,39 +0,0 @@ |
|||||||
package sig.engine; |
|
||||||
|
|
||||||
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; |
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException; |
|
||||||
|
|
||||||
public class Sound{ |
|
||||||
public Sound() { |
|
||||||
try { |
|
||||||
File audioFile = new File("..","Woolookologie.wav"); |
|
||||||
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); |
|
||||||
AudioFormat format = audioStream.getFormat(); |
|
||||||
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format); |
|
||||||
SourceDataLine audioLine = (SourceDataLine)AudioSystem.getLine(info); |
|
||||||
audioLine.open(format); |
|
||||||
audioLine.start(); |
|
||||||
final int BUFFER_SIZE=4096; |
|
||||||
byte[] bytesBuffer = new byte[BUFFER_SIZE]; |
|
||||||
int bytesRead=-1; |
|
||||||
while ((bytesRead=audioStream.read(bytesBuffer))!=-1) { |
|
||||||
audioLine.write(bytesBuffer,0,bytesRead); |
|
||||||
} |
|
||||||
|
|
||||||
audioLine.drain(); |
|
||||||
audioLine.close(); |
|
||||||
audioStream.close(); |
|
||||||
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue