/* * 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 . */ 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); } } }