1 /* AudioPlayerSample.java -- Simple Java Audio Player
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package gnu.classpath.examples.sound;
40 import java.io.IOException;
43 import javax.sound.sampled.AudioFormat;
44 import javax.sound.sampled.AudioInputStream;
45 import javax.sound.sampled.AudioSystem;
46 import javax.sound.sampled.DataLine;
47 import javax.sound.sampled.LineUnavailableException;
48 import javax.sound.sampled.SourceDataLine;
49 import javax.sound.sampled.UnsupportedAudioFileException;
52 * A simple demo to show the use of the Java Sound API.
53 * It plays the given file (up to the end, so don't pass the 26 minutes long
54 * Pink Floyd's Echoes unless you really want!!).
56 * See: http://jsresources.org/examples/SimpleAudioPlayer.java.html
58 * @author Mario Torre <neugens@limasoftware.net>
60 public class AudioPlayerSample
62 private static final int EXTERNAL_BUFFER_SIZE = 128000;
67 public static void main(String[] args)
71 System.out.println("Radio Classpath -: Usage: " +
72 "AudioPlayerSample [file]");
76 String file = args[0];
78 System.out.println("Welcome to Radio Classpath, only great music for you!");
79 System.out.println("Today's DJ Tap The WaterDroplet");
81 // now create the AudioInputStream
82 AudioInputStream audioInputStream = null;
85 audioInputStream = AudioSystem.getAudioInputStream(new File(file));
87 catch (UnsupportedAudioFileException e)
89 // This happen when the subsystem is unable to parse the kind of
90 // audio file we are submitting
91 // See the README for supported audio file types under Classpath
92 // for the version you are using.
102 // get informations about the kind of file we are about to play
103 AudioFormat audioFormat = audioInputStream.getFormat();
105 System.out.println("Playing file: " + file);
106 System.out.println("format: " + audioFormat.toString());
108 System.out.print("Additional properties: ");
110 // now, we try to get all the properties we have in this AudioFormat
112 Map<String, Object> properties = audioFormat.properties();
113 if (properties.size() < 0)
115 System.out.println("none");
119 System.out.println("found #" + properties.size() + " properties");
120 for (String key : properties.keySet())
122 System.out.println(key + ": " + properties.get(key));
126 // let's setup things for playing
127 // first, we require a Line. As we are doing playing, we will ask for a
129 SourceDataLine line = null;
131 // To get the source line, we first need to build an Info object
132 // this is done in one line:
133 DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
135 System.out.println("searching line...");
137 // usually, if a backend can parse a file type, it can also
138 // create a line to handle it, but that's not guaranteed
139 // so we need to take care and to handle a possible
140 // LineUnavailableException
143 line = (SourceDataLine) AudioSystem.getLine(info);
145 System.out.println("line found, opening...");
147 // once created, a line must be opened to let data flow
149 line.open(audioFormat);
151 catch (LineUnavailableException e)
153 // in a real application you should signal that in a kindly way to
164 // an open line pass data to the backend only when it is in
165 // a state called "started" ("playing" or "play" in some other
167 System.out.print("starting line... ");
170 System.out.println("done");
172 // now we can start reading data from the AudioStream and writing
173 // data to the pipeline. The Java Sound API is rather low level
174 // so let you pass up to one byte of data at a time
175 // (with some constraints, refer to the API documentation to know more)
176 // We will do some buffering. You may want to check the frame size
177 // to allow a better buffering, also.
179 System.out.println("now playing...");
182 byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
183 while (nBytesRead != - 1)
187 nBytesRead = audioInputStream.read(abData, 0, abData.length);
189 catch (IOException e)
196 // this method returns the number of bytes actuall written
197 // to the line. You may want to use this number to check
198 // for events, display the current position (give also a
199 // look to the API for other ways of doing that) etc..
200 line.write(abData, 0, nBytesRead);
204 System.out.print("stream finished, draining line... ");
206 // call this method to ensure that all the data in the internal buffer
207 // reach the audio backend, otherwise your application will
208 // cut the last frames of audio data (and users will not enjoy the last
209 // seconds of their precious music)
212 // Once done, we can close the line. Note that a line, once closed
213 // may not be reopened (depends on the backend, in some cases a "reopen",
214 // if allowed, really opens a new line, reallocating all the resources)
216 System.out.println("line drained, now exiting");
219 System.out.println("We hope you enjoyed Radio Classpath!");