1 package se
.umu
.cs
.dit06ajnajs
.util
;
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
;
13 public final class SoundPlayer
{
14 private static boolean mute
= false;
15 private static boolean musicRunning
= true;
16 private static Sequencer sequencer
;
17 private static Thread musicThread
;
20 public static boolean isMute() {
24 public static void setMute(boolean state
) {
26 if (sequencer
.isOpen()) {
36 * Play the supplied clip if mute is not set to true.
38 * @param clip The clip to play.
40 public static void play(final AudioClip clip
) {
41 new Thread(new Runnable() {
51 * Plays the provided Midi-file once
53 * @param midiFile The Midi-file
56 public static void playMusic(final URL midiFile
) {
57 //musicRunning = true;
59 musicThread
= new Thread(new Runnable() {
62 sequencer
= MidiSystem
.getSequencer();
63 sequencer
.setSequence(MidiSystem
.getSequence(midiFile
));
65 sequencer
.setLoopCount(sequencer
.LOOP_CONTINUOUSLY
);
72 if(sequencer
.isRunning()) {
73 Thread
.sleep(1000); // Check every second
74 } else while (!sequencer
.isRunning()) {
75 Thread
.sleep(1000); // Check every second
79 } catch (InterruptedException e
) {
81 } catch (MidiUnavailableException e
) {
82 // TODO Auto-generated catch block
84 } catch (InvalidMidiDataException e
) {
85 // TODO Auto-generated catch block
87 } catch (IOException e
) {
88 // TODO Auto-generated catch block
91 // This is the last thing that runs and then the thread is dead
94 //musicRunning = false;
98 //System.out.println("Starting musicthread");
101 System
.out
.println("Musicthread should not be started, musicRunning is false");
106 public static void killMusic() {
107 //musicRunning = false;
108 if (musicThread
!= null) {
109 System
.out
.println("Interupting musicThread...");
110 musicThread
.interrupt();