jdk/src/share/classes/sun/audio/AudioPlayer.java
author serb
Fri, 29 Mar 2013 22:07:56 +0400
changeset 18215 b2afd66ce6db
parent 5506 202f599c92aa
child 25091 e69235b071ef
permissions -rw-r--r--
8006328: Improve robustness of sound classes 8009057: Improve MIDI event handling Reviewed-by: amenkov, art, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.audio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class provides an interface to play audio streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * To play an audio stream use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *      AudioPlayer.player.start(audiostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * To stop playing an audio stream use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      AudioPlayer.player.stop(audiostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * To play an audio stream from a URL use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      AudioStream audiostream = new AudioStream(url.openStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      AudioPlayer.player.start(audiostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * To play a continuous sound you first have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * create an AudioData instance and use it to construct a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ContinuousAudioDataStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *      AudioData data = new AudioStream(url.openStream()).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      AudioPlayer.player.start(audiostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see AudioData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see AudioDataStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see AudioDevice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see AudioStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Arthur van Hoff, Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    66
public final class AudioPlayer extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    68
        private final AudioDevice devAudio;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    69
        private final static boolean DEBUG = false /*true*/;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * The default audio player. This audio player is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public static final AudioPlayer player = getAudioPlayer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private static ThreadGroup getAudioThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if(DEBUG) { System.out.println("AudioPlayer.getAudioThreadGroup()"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            ThreadGroup g = currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            while ((g.getParent() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                   (g.getParent().getParent() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                g = g.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * Create an AudioPlayer thread in a privileged block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        private static AudioPlayer getAudioPlayer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if(DEBUG) { System.out.println("> AudioPlayer.getAudioPlayer()"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            AudioPlayer audioPlayer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        Thread t = new AudioPlayer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        t.setPriority(MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            audioPlayer = (AudioPlayer) AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return audioPlayer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * Construct an AudioPlayer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private AudioPlayer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            super(getAudioThreadGroup(), "Audio Player");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if(DEBUG) { System.out.println("> AudioPlayer private constructor"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            devAudio = AudioDevice.device;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            devAudio.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if(DEBUG) { System.out.println("< AudioPlayer private constructor completed"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * Start playing a stream. The stream will continue to play
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * until the stream runs out of data, or it is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * @see AudioPlayer#stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public synchronized void start(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println("> AudioPlayer.start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println("  InputStream = " + in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            devAudio.openChannel(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                System.out.println("< AudioPlayer.start completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Stop playing a stream. The stream will stop playing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * nothing happens if the stream wasn't playing in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * first place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * @see AudioPlayer#start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        public synchronized void stop(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                System.out.println("> AudioPlayer.stop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            devAudio.closeChannel(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                System.out.println("< AudioPlayer.stop completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Main mixing loop. This is called automatically when the AudioPlayer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // $$jb: 06.24.99: With the JS API, mixing is no longer done by AudioPlayer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // or AudioDevice ... it's done by the API itself, so this bit of legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // code does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // $$jb: 10.21.99: But it appears that some legacy applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // check to see if this thread is alive or not, so we need to spin here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            devAudio.play();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                System.out.println("AudioPlayer mixing loop.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    Thread.sleep(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    //wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    // interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if(DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                System.out.println("AudioPlayer exited.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }