001: /*
002: * @(#)AudioPlayer.java 1.41 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.audio;
029:
030: import java.util.Vector;
031: import java.util.Enumeration;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035: import java.security.AccessController;
036: import java.security.PrivilegedAction;
037:
038: /**
039: * This class provides an interface to play audio streams.
040: *
041: * To play an audio stream use:
042: * <pre>
043: * AudioPlayer.player.start(audiostream);
044: * </pre>
045: * To stop playing an audio stream use:
046: * <pre>
047: * AudioPlayer.player.stop(audiostream);
048: * </pre>
049: * To play an audio stream from a URL use:
050: * <pre>
051: * AudioStream audiostream = new AudioStream(url.openStream());
052: * AudioPlayer.player.start(audiostream);
053: * </pre>
054: * To play a continuous sound you first have to
055: * create an AudioData instance and use it to construct a
056: * ContinuousAudioDataStream.
057: * For example:
058: * <pre>
059: * AudoData data = new AudioStream(url.openStream()).getData();
060: * ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
061: * AudioPlayer.player.stop(audiostream);
062: * </pre>
063: *
064: * @see AudioData
065: * @see AudioDataStream
066: * @see AudioDevice
067: * @see AudioStream
068: * @author Arthur van Hoff, Thomas Ball
069: * @version 1.37, 08/19/02
070: */
071: public class AudioPlayer extends Thread {
072: private AudioDevice devAudio;
073: /**
074: * The default audio player. This audio player is initialized
075: * automatically.
076: */
077: public static final AudioPlayer player = getAudioPlayer();
078:
079: private static ThreadGroup getAudioThreadGroup() {
080: ThreadGroup g = currentThread().getThreadGroup();
081: while ((g.getParent() != null)
082: && (g.getParent().getParent() != null)) {
083: g = g.getParent();
084: }
085: return g;
086: }
087:
088: /**
089: * Create an AudioPlayer thread in a privileged block.
090: */
091: private static AudioPlayer getAudioPlayer() {
092: return (AudioPlayer) AccessController
093: .doPrivileged(new PrivilegedAction() {
094: public Object run() {
095: return new AudioPlayer();
096: }
097: });
098: }
099:
100: /**
101: * Construct an AudioPlayer.
102: */
103: private AudioPlayer() {
104: super (getAudioThreadGroup(), "Audio Player");
105: devAudio = AudioDevice.device;
106: setPriority(MAX_PRIORITY);
107: setDaemon(true);
108: start();
109: }
110:
111: /**
112: * Start playing a stream. The stream will continue to play
113: * until the stream runs out of data, or it is stopped.
114: * @see AudioPlayer#stop
115: */
116: public synchronized void start(InputStream in) {
117: devAudio.openChannel(in);
118: notify();
119: }
120:
121: /**
122: * Stop playing a stream. The stream will stop playing,
123: * nothing happens if the stream wasn't playing in the
124: * first place.
125: * @see AudioPlayer#start
126: */
127: public synchronized void stop(InputStream in) {
128: devAudio.closeChannel(in);
129: }
130:
131: /**
132: * Main mixing loop. This is called automatically when the AudioPlayer
133: * is created.
134: */
135: public void run() {
136: devAudio.open();
137: devAudio.play();
138: devAudio.close();
139: System.out.println("audio player exit");
140: }
141: }
|