Fixed bug in collecting credits and changed behaviour of playing music
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / ATDSoundPlayer.java
blobc97f50fe63f6fccb9c6a4e72fba332d69f070aa9
1 package se.umu.cs.dit06ajnajs;
3 import java.applet.AudioClip;
4 import java.io.File;
5 import java.io.IOException;
7 import javax.sound.midi.InvalidMidiDataException;
8 import javax.sound.midi.MidiSystem;
9 import javax.sound.midi.MidiUnavailableException;
10 import javax.sound.midi.Sequencer;
12 public final class ATDSoundPlayer {
13 private static boolean mute = false;
14 private static boolean musicRunning = true;
15 private static Sequencer sequencer;
16 private static Thread musicThread;
19 public static boolean isMute() {
20 return mute;
23 public static void setMute(boolean state) {
24 mute = state;
25 if (sequencer.isOpen()) {
26 if (mute) {
27 sequencer.stop();
28 } else {
29 sequencer.start();
34 /**
35 * Play the supplied clip if mute is not set to true.
37 * @param clip The clip to play.
39 public static void play(final AudioClip clip) {
40 new Thread(new Runnable() {
41 public void run() {
42 if (!mute) {
43 clip.play();
46 }).start();
49 /**
50 * Plays the provided Midi-file once
52 * @param file The Midi-file
54 // TODO loop the midi
55 public static void playMusic(final File midiFile) {
56 //musicRunning = true;
57 // Play once
58 musicThread = new Thread(new Runnable() {
59 public void run() {
60 try {
61 sequencer = MidiSystem.getSequencer();
62 sequencer.setSequence(MidiSystem.getSequence(midiFile));
63 sequencer.open();
64 sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
65 if (!mute) {
66 sequencer.start();
69 while(true) {
70 // Check if running
71 if(sequencer.isRunning()) {
72 Thread.sleep(1000); // Check every second
73 } else while (!sequencer.isRunning()) {
74 Thread.sleep(1000); // Check every second
78 } catch (InterruptedException e) {
80 } catch (MidiUnavailableException e) {
81 // TODO Auto-generated catch block
82 e.printStackTrace();
83 } catch (InvalidMidiDataException e) {
84 // TODO Auto-generated catch block
85 e.printStackTrace();
86 } catch (IOException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
90 // This is the last thing that runs and then the thread is dead
91 sequencer.stop();
92 sequencer.close();
93 //musicRunning = false;
95 });
96 if (true) {
97 System.out.println("Starting musicthread");
98 musicThread.start();
99 } else {
100 System.out.println("Musicthread should not be started, musicRunning is false");
105 public static void killMusic() {
106 //musicRunning = false;
107 if (musicThread != null) {
108 System.out.println("Interupting musicThread...");
109 musicThread.interrupt();
110 musicRunning = true;