summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java')
-rw-r--r--src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java b/src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java
new file mode 100644
index 0000000..23d6a6f
--- /dev/null
+++ b/src/com/joshwalters/bookcatalog/sound/PlayWavSoundFile.java
@@ -0,0 +1,78 @@
+/*
+ * Book Catalog - Catalog your book collection.
+ *
+ * Copyright (C) 2009 Joshua Walters
+ * URL: http://joshwalters.com
+ *
+ * This file is part of Book Catalog.
+ *
+ * Book Catalog is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Book Catalog is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Book Catalog. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.joshwalters.bookcatalog.sound;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import sun.audio.AudioPlayer;
+import sun.audio.AudioStream;
+
+/**
+ * Plays a WAV file in a separate thread. This allows for very simple
+ * asynchronous WAV playback.
+ *
+ * @author Josh Walters
+ */
+public class PlayWavSoundFile extends Thread {
+
+ /**
+ * The WAV file to be played.
+ */
+ private String filename;
+
+ /**
+ * Takes the file name of the WAV file to be played.
+ *
+ * @param wavfile
+ * The WAV file to be played.
+ */
+ public PlayWavSoundFile(String wavfile) {
+ filename = wavfile;
+ }
+
+ /**
+ * Plays the WAV file. First checks to see if the file is in a JAR with the
+ * program, then checks to see if it is present in a regular folder/file.
+ * Playback is asynchronous.
+ */
+ public void run() {
+ try {
+ // Look for the WAV in a JAR
+ InputStream in = getClass().getClassLoader().getResourceAsStream(
+ filename);
+ // If not in JAR, check for regular file/folder
+ if (in == null) {
+ in = new FileInputStream(filename);
+ }
+ // Get the audio, and play it
+ AudioStream as = new AudioStream(in);
+ AudioPlayer.player.start(as);
+ } catch (IOException e) {
+ // Print the error to error output
+ System.err.println("Error loading and playing sound file: "
+ + filename);
+ }
+ }
+} \ No newline at end of file