Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Tweaks for sound play
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Oct 30, 2021
1 parent ec562f3 commit 8843675
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/main/java/io/github/eb4j/ebview/gui/LinkActionListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.SwingWorker;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
Expand All @@ -21,16 +23,29 @@ private static boolean hasExt(final String path, final String[] extrn) {
}

public static synchronized void playSound(final File file) {
new Thread(() -> {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
new SwingWorker<Void, Void>() {
private Clip clip;
private AudioInputStream inputStream;

@Override
protected Void doInBackground() throws Exception {
clip = AudioSystem.getClip();
inputStream = AudioSystem.getAudioInputStream(file);
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
clip.drain();
return null;
}

@Override
protected void done() {
try {
inputStream.close();
} catch (IOException ignored) {
}
clip.close();
}
}).start();
}.execute();
}

@Override
Expand All @@ -39,9 +54,9 @@ public void hyperlinkUpdate(final HyperlinkEvent hyperlinkEvent) {
URL url = hyperlinkEvent.getURL();
if (url.getProtocol().equals("file")) {
try {
String path = url.toURI().getPath();
String path = url.getPath();
if (path.endsWith(".wav") || path.endsWith(".WAV")) {
playSound(new File(path));
playSound(new File(url.toURI()));
} else if (hasExt(path, MOVIE_EXTS)) {
MoviePlay player = new MoviePlay(354, 280);
player.play(path);
Expand Down

0 comments on commit 8843675

Please sign in to comment.