Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
MP3.java
Go to the documentation of this file.
1 package micropolisj.util;
2 
3 /*************************************************************************
4  * Compilation: javac -classpath .:jl1.0.jar MP3.java (OS X)
5  * javac -classpath .;jl1.0.jar MP3.java (Windows)
6  * Execution: java -classpath .:jl1.0.jar MP3 filename.mp3 (OS X / Linux)
7  * java -classpath .;jl1.0.jar MP3 filename.mp3 (Windows)
8  *
9  * Plays an MP3 file using the JLayer MP3 library.
10  *
11  * Reference: http://www.javazoom.net/javalayer/sources.html
12  *
13  *
14  * To execute, get the file jl1.0.jar from the website above or from
15  *
16  * http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
17  *
18  * and put it in your working directory with this file MP3.java.
19  *
20  *************************************************************************/
21 
22 import java.io.BufferedInputStream;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.net.URL;
26 
27 import javazoom.jl.player.advanced.AdvancedPlayer;
28 import javazoom.jl.player.advanced.PlaybackEvent;
29 import javazoom.jl.player.advanced.PlaybackListener;
30 
31 public class MP3 extends PlaybackListener {
32  private String filename;
33  private AdvancedPlayer player;
34  private boolean loop;
35  private volatile Thread thread;
36 
37  // CONSTRUCTORS
38  public MP3(String filename) {
39  this(filename, false);
40  }
41 
42  public MP3(URL url) {
43  this(url, false);
44  }
45 
46  public MP3(URL url, boolean loop) {
47  this(url.toString().substring(5), loop);
48 // this(url.toString(), loop);
49  }
50 
51  public MP3(String filename, boolean loop) {
52  this.filename = filename;
53  this.player = null;
54  this.loop = loop;
55  this.thread = null;
56  }
57 
58  // METHODS
59 
60  public void close() {
61  if(player != null) {
62  player.close();
63  }
64 
65  if(thread != null) {
66  thread.interrupt();
67  thread = null;
68  }
69  }
70 
71  public AdvancedPlayer createPlayer() {
72  close();
73  try {
74  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
75  AdvancedPlayer p = new AdvancedPlayer(bis);
76 
77  if(loop) {
78  p.setPlayBackListener(new LoopListener());
79  }
80  else {
81  p.setPlayBackListener(new NonLoopListener());
82  }
83  return p;
84  }
85  catch(Exception e) {
86  System.out.println("Problem with file " + filename);
87  System.out.println(e);
88  return null;
89  }
90  }
91 
92  // play the MP3 file to the sound card
93  public void play() {
94  player = createPlayer();
95  // run in new thread to play in background
96  thread = new Thread() {
97  public void run() {
98  try {
99  player.play();
100  }
101  catch(Exception e) {
102  System.err.println(e);
103  }
104  }
105  };
106  thread.start();
107  }
108 
109  private class LoopListener extends PlaybackListener {
110  @Override
111  public void playbackFinished(PlaybackEvent evt) {
112  play();
113  }
114  }
115 
116  private class NonLoopListener extends PlaybackListener {
117  @Override
118  public void playbackFinished(PlaybackEvent evt) {
119  thread.interrupt();
120  thread = null;
121  }
122  }
123 }