Skip to content
Permalink
Browse files

Use AtomicReference

  • Loading branch information...
danielb987 committed Oct 29, 2018
1 parent 91be9d3 commit 2dbb59c6ae07ec2ea98b6850cb38160ea5b5b12f
Showing with 38 additions and 23 deletions.
  1. +38 −23 java/src/jmri/jmrit/Sound.java
@@ -7,6 +7,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
@@ -42,9 +43,8 @@
private final URL url;
private boolean streaming = false;
private boolean streamingStop = false;
private volatile Clip clip = null;
private AtomicReference<Clip> clipRef = new AtomicReference<>();
private boolean autoClose = true;
private final Object lock = new Object();
private final static Logger log = LoggerFactory.getLogger(Sound.class);

/**
@@ -83,7 +83,9 @@ public Sound(@Nonnull URL url) throws NullPointerException {
try {
streaming = this.needStreaming();
if (!streaming) {
openClip();
clipRef.updateAndGet(clip -> {
return openClip();
});
}
} catch (URISyntaxException ex) {
streaming = false;
@@ -92,27 +94,32 @@ public Sound(@Nonnull URL url) throws NullPointerException {
}
}

private void openClip() {
private Clip openClip() {
Clip newClip = null;
try {
clip = AudioSystem.getClip();
clip.addLineListener(event -> {
newClip = AudioSystem.getClip();
newClip.addLineListener(event -> {
if (LineEvent.Type.STOP.equals(event.getType())) {
if (autoClose) {
synchronized(lock) {
clip.close();
clip = null;
}
clipRef.updateAndGet(clip -> {
if (clip != null) {
clip.close();
}
return null;
});
}
}
});
clip.open(AudioSystem.getAudioInputStream(url));
newClip.open(AudioSystem.getAudioInputStream(url));
} catch (IOException ex) {
log.error("Unable to open {}", url);
} catch (LineUnavailableException ex) {
log.error("Unable to provide audio playback", ex);
} catch (UnsupportedAudioFileException ex) {
log.error("{} is not a recognised audio format", url);
}

return newClip;
}

/**
@@ -138,24 +145,25 @@ public void close() {
if (streaming) {
streamingStop = true;
} else {
synchronized(lock) {
clipRef.updateAndGet(clip -> {
if (clip != null) {
clip.close();
clip = null;
}
}
return null;
});
}
}

@Override
public void finalize() throws Throwable {
try {
if (!streaming) {
synchronized(lock) {
clipRef.updateAndGet(clip -> {
if (clip != null) {
clip.close();
}
}
return null;
});
}
} finally {
super.finalize();
@@ -171,14 +179,15 @@ public void play() {
Thread tStream = new Thread(streamSound);
tStream.start();
} else {
synchronized(lock) {
clipRef.updateAndGet(clip -> {
if (clip == null) {
openClip();
clip = openClip();
}
if (clip != null) {
clip.start();
}
}
return clip;
});
}
}

@@ -200,14 +209,15 @@ public void loop(int count) {
if (streaming) {
log.warn("Streaming this audio file, loop() not allowed");
} else {
synchronized(lock) {
clipRef.updateAndGet(clip -> {
if (clip == null) {
openClip();
clip = openClip();
}
if (clip != null) {
clip.loop(count);
}
}
return clip;
});
}
}

@@ -218,7 +228,12 @@ public void stop() {
if (streaming) {
streamingStop = true;
} else {
this.clip.stop();
clipRef.updateAndGet(clip -> {
if (clip != null) {
clip.stop();
}
return clip;
});
}
}

0 comments on commit 2dbb59c

Please sign in to comment.
You can’t perform that action at this time.