1 package se
.umu
.cs
.dit06ajnajs
;
3 import java
.applet
.AudioClip
;
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() {
23 public static void setMute(boolean state
) {
25 if (sequencer
.isOpen()) {
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() {
50 * Plays the provided Midi-file once
52 * @param file The Midi-file
55 public static void playMusic(final File midiFile
) {
56 //musicRunning = true;
58 musicThread
= new Thread(new Runnable() {
61 sequencer
= MidiSystem
.getSequencer();
62 sequencer
.setSequence(MidiSystem
.getSequence(midiFile
));
64 sequencer
.setLoopCount(sequencer
.LOOP_CONTINUOUSLY
);
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
83 } catch (InvalidMidiDataException e
) {
84 // TODO Auto-generated catch block
86 } catch (IOException e
) {
87 // TODO Auto-generated catch block
90 // This is the last thing that runs and then the thread is dead
93 //musicRunning = false;
97 System
.out
.println("Starting musicthread");
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();