KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AudioFilePlayer


1 /*
2  * SNMP Inquisitor
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30 import java.io.*;
31 import javax.sound.sampled.*;
32 import java.util.jar.*;
33
34
35 /**
36 * Utility class giving a convenient interface for playing sound from a file. Note
37 * that this requires the javax.sound.sampled.* classes that are part of jdk1.3 and up;
38 * as such, you should test the version of Java to make sure it's high enough to support
39 * this - e.g.,
40 *
41 * String version = System.getProperty("java.version");
42 *
43 * if (version.compareTo("1.3") >= 0)
44 * {
45 * AudioFilePlayer audioPlayer = new AudioFilePlayer("thisJarFile.jar", "mySound.wav");
46 * audioPlayer.playFromJarFile();
47 * }
48 *
49 **/

50
51 public class AudioFilePlayer
52 {
53     
54     private String JavaDoc soundFileName, jarFileName;
55     
56     
57     /**
58     * Create a player for the audio file whose pathname is supplied.
59     * Note: used with standard file-system file, not jar file.
60     **/

61     
62     public AudioFilePlayer(String JavaDoc soundFileName)
63     {
64         this.soundFileName = soundFileName;
65     }
66     
67     
68     
69     /**
70     * Create a player for the specified audio file contained in the specified jar file.
71     * Note that the jar file may be the one containing the application code.
72     **/

73     
74     public AudioFilePlayer(String JavaDoc jarFileName, String JavaDoc soundFileName)
75     {
76         this.jarFileName = jarFileName;
77         this.soundFileName = soundFileName;
78     }
79     
80     
81     
82     /**
83     * Play the associated audio file contained in the associated jar file.
84     **/

85     
86     public void playFromJarFile()
87     {
88     
89         try
90         {
91             JarFile thisJarFile = new JarFile(jarFileName);
92             
93             JarEntry audioEntry = thisJarFile.getJarEntry(soundFileName);
94             
95             // need an input stream supporting mark() and reset(); used BufferedInputStream
96
BufferedInputStream jarFileInputStream = new BufferedInputStream(thisJarFile.getInputStream(audioEntry));
97             
98             // get the audio file format; think this is broken...
99
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(jarFileInputStream);
100             
101             // ..so the following doesn't work
102
//AudioFormat audioFormat = audioFileFormat.getFormat();
103

104             // so get the audio format hard-coded; wish the preceding commented-out line worked...
105
AudioFormat audioFormat = new AudioFormat(11025, 16, 1, true, false);
106             
107             DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat);
108             
109             Clip audioClip = (Clip)AudioSystem.getLine(dataLineInfo);
110             
111             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(jarFileInputStream);
112             
113             audioClip.open(audioInputStream);
114             
115             audioClip.start();
116         }
117         catch (Exception JavaDoc e)
118         {
119             // do nothing...
120
System.out.println(e);
121         }
122     
123     }
124     
125     
126     
127     /**
128     * Play the associated audio file.
129     **/

130     
131     public void playFromFile()
132     {
133     
134         try
135         {
136             File audioFile = new File(soundFileName);
137             
138             AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(audioFile);
139             
140             AudioFormat audioFormat = audioFileFormat.getFormat();
141                     
142             DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat);
143             
144             Clip audioClip = (Clip)AudioSystem.getLine(dataLineInfo);
145             
146             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile);
147             
148             audioClip.open(audioInputStream);
149             
150             audioClip.start();
151         }
152         catch (Exception JavaDoc e)
153         {
154             // do nothing...
155
System.out.println(e);
156         }
157     
158     }
159
160 }
Popular Tags