\r
import java.io.BufferedInputStream;\r
import java.io.IOException;\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
+import java.util.concurrent.atomic.AtomicInteger;\r
\r
import javax.sound.sampled.AudioFormat;\r
import javax.sound.sampled.AudioInputStream;\r
import com.badlogic.gdx.audio.Music;\r
\r
public class JoglMusic implements Music, Runnable {\r
- private enum State {\r
- Playing, Stopped, Paused\r
- }\r
+ private final int Playing = 0;\r
+ private final int Stopped = 1;\r
+ private final int Paused = 2;\r
\r
- private State state = State.Stopped;\r
+ private AtomicInteger state = new AtomicInteger(Stopped);\r
private final Thread thread;\r
private final JoglFileHandle handle;\r
private AudioInputStream ain;\r
private final SourceDataLine line;\r
private final byte[] buffer;\r
- private boolean looping = false;\r
+ private AtomicBoolean looping = new AtomicBoolean(false);\r
private boolean disposed = false;\r
\r
public JoglMusic (JoglFileHandle handle) throws UnsupportedAudioFileException, IOException, LineUnavailableException {\r
}\r
\r
@Override public boolean isLooping () {\r
- return looping;\r
+ return looping.get();\r
}\r
\r
@Override public boolean isPlaying () {\r
- return state == State.Playing;\r
+ return state.get() == Playing;\r
}\r
\r
@Override public void pause () {\r
- synchronized (this) {\r
- if (state == State.Playing) state = State.Paused;\r
- }\r
+ state.compareAndSet(Playing, Paused);\r
}\r
\r
@Override public void play () {\r
- synchronized (this) {\r
- if (state == State.Playing) return;\r
-\r
- if (state == State.Paused) {\r
- state = State.Playing;\r
- return;\r
- }\r
-\r
- try {\r
- openAudioInputStream();\r
- state = State.Playing;\r
- } catch (Exception e) {\r
- state = State.Stopped;\r
- }\r
- }\r
+ state.set(Playing);\r
}\r
\r
@Override public void stop () {\r
- synchronized (this) {\r
- if (state == State.Stopped) return;\r
-\r
- state = State.Stopped;\r
- line.flush();\r
- try {\r
- ain.close();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
- ain = null;\r
- }\r
+ state.set(Stopped);\r
}\r
\r
@Override public void setLooping (boolean isLooping) {\r
- looping = isLooping;\r
+ looping.set(isLooping);\r
}\r
\r
@Override public void setVolume (float volume) {\r
long readSamples = 0;\r
\r
while (!disposed) {\r
- synchronized (this) {\r
- if (state == State.Playing) {\r
+ int curState = state.get();\r
+ if (curState == Playing) {\r
+ try {\r
+ if (ain == null) openAudioInputStream();\r
+ readBytes = ain.read(buffer);\r
+\r
+ if (readBytes != -1) {\r
+ int writtenBytes = line.write(buffer, 0, readBytes);\r
+ while (writtenBytes != readBytes)\r
+ writtenBytes += line.write(buffer, writtenBytes, readBytes - writtenBytes);\r
+ readSamples += readBytes / ain.getFormat().getFrameSize();\r
+ } else {\r
+ System.out.println("samples: " + readSamples);\r
+ ain.close();\r
+ if (!isLooping()) state.set(Stopped);\r
+ else openAudioInputStream();\r
+ }\r
+ } catch (Exception ex) {\r
try {\r
-\r
- readBytes = ain.read(buffer);\r
-\r
- if (readBytes != -1) {\r
- int writtenBytes = line.write(buffer, 0, readBytes);\r
- while (writtenBytes != readBytes)\r
- writtenBytes += line.write(buffer, writtenBytes, readBytes - writtenBytes);\r
- readSamples += readBytes / ain.getFormat().getFrameSize();\r
- } else {\r
- System.out.println("samples: " + readSamples);\r
- ain.close();\r
- if (!isLooping())\r
- state = State.Stopped;\r
- else\r
- openAudioInputStream();\r
- }\r
- } catch (Exception ex) {\r
- try {\r
- ain.close();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
- line.close();\r
- ex.printStackTrace();\r
- state = State.Stopped;\r
- return;\r
+ ain.close();\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
}\r
+ line.close();\r
+ ex.printStackTrace();\r
+ state.set(Stopped);\r
+ return;\r
}\r
-\r
+ }\r
+ else if (curState == Stopped && ain != null)\r
+ {\r
try {\r
- Thread.sleep(10);\r
- } catch (InterruptedException e) {\r
- // TODO Auto-generated catch block\r
+ ain.close();\r
+ } catch (IOException e) {\r
e.printStackTrace();\r
}\r
+ ain = null;\r
+ line.flush();\r
+ }\r
+\r
+ try {\r
+ Thread.sleep(10);\r
+ } catch (InterruptedException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
}\r
}\r
}\r