MusicPlayer.java
1    import javazoom.jl.decoder.JavaLayerException;
2    import javazoom.jl.player.AudioDevice;
3    import javazoom.jl.player.FactoryRegistry;
4    import javazoom.jl.player.advanced.AdvancedPlayer;
5    import java.io.BufferedInputStream;
6    import java.io.FileInputStream;
7    import java.io.IOException;
8    import java.io.InputStream;
9    import java.util.HashMap;
10   
11   /* 
12    * @author David J. Barnes und Michael Kölling 
13    * @version 31.07.2011 
14    */
15   
16   public class MusicPlayer{
17   
18       private AdvancedPlayer player;
19       private HashMap<String, String> notes;
20   
21       public MusicPlayer(){
22           player = null;
23           notes = new HashMap<>();
24           getNotes().put("C", "res/SynthesizedPianoNotes/Piano11.mp3");
25           getNotes().put("C#", "res/SynthesizedPianoNotes/Piano12.mp3");
26           getNotes().put("D", "res/SynthesizedPianoNotes/Piano13.mp3");
27           getNotes().put("D#", "res/SynthesizedPianoNotes/Piano14.mp3");
28           getNotes().put("E", "res/SynthesizedPianoNotes/Piano15.mp3");
29           getNotes().put("F", "res/SynthesizedPianoNotes/Piano16.mp3");
30           getNotes().put("F#", "res/SynthesizedPianoNotes/Piano17.mp3");
31           getNotes().put("G", "res/SynthesizedPianoNotes/Piano18.mp3");
32           getNotes().put("G#", "res/SynthesizedPianoNotes/Piano19.mp3");
33           getNotes().put("A", "res/SynthesizedPianoNotes/Piano110.mp3");
34           getNotes().put("A#", "res/SynthesizedPianoNotes/Piano111.mp3");
35           getNotes().put("B", "res/SynthesizedPianoNotes/Piano112.mp3");
36           getNotes().put("C'", "res/SynthesizedPianoNotes/Piano113.mp3");
37           getNotes().put("C#'", "res/SynthesizedPianoNotes/Piano114.mp3");
38           getNotes().put("D'", "res/SynthesizedPianoNotes/Piano115.mp3");
39           getNotes().put("D#'", "res/SynthesizedPianoNotes/Piano116.mp3");
40           getNotes().put("E'", "res/SynthesizedPianoNotes/Piano117.mp3");
41           getNotes().put("F'", "res/SynthesizedPianoNotes/Piano118.mp3");
42           getNotes().put("F#'", "res/SynthesizedPianoNotes/Piano119.mp3");
43           getNotes().put("G'", "res/SynthesizedPianoNotes/Piano120.mp3");
44           getNotes().put("G#'", "res/SynthesizedPianoNotes/Piano121.mp3");
45           getNotes().put("A'", "res/SynthesizedPianoNotes/Piano122.mp3");
46           getNotes().put("A#'", "res/SynthesizedPianoNotes/Piano123.mp3");
47           getNotes().put("B'", "res/SynthesizedPianoNotes/Piano124.mp3");
48           getNotes().put("click", "res/SynthesizedPianoNotes/click.mp3");
49       }
50   
51       public void playNote(final String path, int duration){
52           try{
53               preparePlayer(path);
54               Thread playerThread = new Thread(){
55                   public void run(){
56                       try{
57                           player.play(duration);
58                       } catch(JavaLayerException e){
59                           outputProblem(path);
60                       } finally{
61                           killPlayer();
62                       }
63                   }
64               };
65               playerThread.start();
66           } catch (Exception ex){
67               outputProblem(path);
68           }
69       }
70   
71       public void stop(){
72           killPlayer();
73       }
74   
75       private void preparePlayer(String path){
76           try{
77               InputStream is = returnInputStream(path);
78               player = new AdvancedPlayer(is, createAudioDevice());
79           } catch (IOException | JavaLayerException e){
80               outputProblem(path);
81               killPlayer();
82           }
83       }
84   
85       private InputStream returnInputStream(String path) throws IOException{
86           return new BufferedInputStream(
87                   new FileInputStream(path));
88       }
89   
90       private AudioDevice createAudioDevice() throws JavaLayerException{
91           return FactoryRegistry.systemRegistry().createAudioDevice();
92       }
93   
94       private void killPlayer(){
95           synchronized(this){
96               if(player != null){
97                   player.stop();
98                   player = null;
99               }
100          }
101      }
102  
103      private void outputProblem(String path){
104          System.out.println("There was a problem with playing: " + path);
105      }
106  
107      public HashMap<String, String> getNotes(){
108          return this.notes;
109      }
110  
111      public void setNotes(HashMap<String, String> notes){
112          this.notes = notes;
113      }
114  }