blob: 23d6a6f21cf15a3af112efc108da867eae3db550 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);
}
}
}
|