Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/audio/AudioPlayer.java
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.audio;2627import java.io.InputStream;28import java.security.AccessController;29import java.security.PrivilegedAction;303132/**33* This class provides an interface to play audio streams.34*35* To play an audio stream use:36* <pre>37* AudioPlayer.player.start(audiostream);38* </pre>39* To stop playing an audio stream use:40* <pre>41* AudioPlayer.player.stop(audiostream);42* </pre>43* To play an audio stream from a URL use:44* <pre>45* AudioStream audiostream = new AudioStream(url.openStream());46* AudioPlayer.player.start(audiostream);47* </pre>48* To play a continuous sound you first have to49* create an AudioData instance and use it to construct a50* ContinuousAudioDataStream.51* For example:52* <pre>53* AudioData data = new AudioStream(url.openStream()).getData();54* ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);55* AudioPlayer.player.start(audiostream);56* </pre>57*58* @see AudioData59* @see AudioDataStream60* @see AudioDevice61* @see AudioStream62* @author Arthur van Hoff, Thomas Ball63*/6465public final class AudioPlayer extends Thread {6667private final AudioDevice devAudio;68private final static boolean DEBUG = false /*true*/;6970/**71* The default audio player. This audio player is initialized72* automatically.73*/74public static final AudioPlayer player = getAudioPlayer();7576private static ThreadGroup getAudioThreadGroup() {7778if(DEBUG) { System.out.println("AudioPlayer.getAudioThreadGroup()"); }79ThreadGroup g = currentThread().getThreadGroup();80while ((g.getParent() != null) &&81(g.getParent().getParent() != null)) {82g = g.getParent();83}84return g;85}8687/**88* Create an AudioPlayer thread in a privileged block.89*/9091private static AudioPlayer getAudioPlayer() {9293if(DEBUG) { System.out.println("> AudioPlayer.getAudioPlayer()"); }94AudioPlayer audioPlayer;95PrivilegedAction action = new PrivilegedAction() {96public Object run() {97Thread t = new AudioPlayer();98t.setPriority(MAX_PRIORITY);99t.setDaemon(true);100t.start();101return t;102}103};104audioPlayer = (AudioPlayer) AccessController.doPrivileged(action);105return audioPlayer;106}107108/**109* Construct an AudioPlayer.110*/111private AudioPlayer() {112113super(getAudioThreadGroup(), "Audio Player");114if(DEBUG) { System.out.println("> AudioPlayer private constructor"); }115devAudio = AudioDevice.device;116devAudio.open();117if(DEBUG) { System.out.println("< AudioPlayer private constructor completed"); }118}119120121/**122* Start playing a stream. The stream will continue to play123* until the stream runs out of data, or it is stopped.124* @see AudioPlayer#stop125*/126public synchronized void start(InputStream in) {127128if(DEBUG) {129System.out.println("> AudioPlayer.start");130System.out.println(" InputStream = " + in);131}132devAudio.openChannel(in);133notify();134if(DEBUG) {135System.out.println("< AudioPlayer.start completed");136}137}138139/**140* Stop playing a stream. The stream will stop playing,141* nothing happens if the stream wasn't playing in the142* first place.143* @see AudioPlayer#start144*/145public synchronized void stop(InputStream in) {146147if(DEBUG) {148System.out.println("> AudioPlayer.stop");149}150151devAudio.closeChannel(in);152if(DEBUG) {153System.out.println("< AudioPlayer.stop completed");154}155}156157/**158* Main mixing loop. This is called automatically when the AudioPlayer159* is created.160*/161public void run() {162163// $$jb: 06.24.99: With the JS API, mixing is no longer done by AudioPlayer164// or AudioDevice ... it's done by the API itself, so this bit of legacy165// code does nothing.166// $$jb: 10.21.99: But it appears that some legacy applications167// check to see if this thread is alive or not, so we need to spin here.168169devAudio.play();170if(DEBUG) {171System.out.println("AudioPlayer mixing loop.");172}173while(true) {174try{175Thread.sleep(5000);176//wait();177} catch(Exception e) {178break;179// interrupted180}181}182if(DEBUG) {183System.out.println("AudioPlayer exited.");184}185186}187}188189190