|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package javax.sound; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +import com.sun.media.sound.JavaSoundAudioClip; |
| 32 | + |
| 33 | +/** |
| 34 | + * The {@code SoundClip} class is a simple abstraction for playing a sound clip. |
| 35 | + * It will play any format that is recognized by the {@code javax.sound} API, |
| 36 | + * and for which it has support. This includes midi data. |
| 37 | + * <p> |
| 38 | + * This class is intended for easy playback of short clips or snippets of sound. |
| 39 | + * Examples of when this might be used is to play an audible alert or effect in a UI app, |
| 40 | + * or to make a short announcement or to provide audible feedback such announcing as the |
| 41 | + * function of a button or control. |
| 42 | + * The application will typically let such clips play once to completion. |
| 43 | + * <p> |
| 44 | + * Applications needing more precise control or advanced |
| 45 | + * features should look into other parts of the {@code javax.sound} API. |
| 46 | + * Playing sound requires that the environment grants access to audio devices. |
| 47 | + * Typically this means running the application in a desktop environment. |
| 48 | + * <p> |
| 49 | + * Multiple {@code SoundClip} items can be playing at the same time, and |
| 50 | + * the resulting sound is mixed together to produce a composite. |
| 51 | + * |
| 52 | + * @since 25 |
| 53 | + */ |
| 54 | +public final class SoundClip { |
| 55 | + |
| 56 | + private final JavaSoundAudioClip clip; |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a {@code SoundClip} instance which will play a clip from the supplied file. |
| 60 | + * <p> |
| 61 | + * The file contents will be fully read before this method returns. |
| 62 | + * If the file does not contain recognizable and supported sound data, or |
| 63 | + * if the implementation does not find a suitable output device for the data, |
| 64 | + * playing the clip will be a no-op. |
| 65 | + * |
| 66 | + * @param file the file from which to obtain the sound data |
| 67 | + * @return a {@code SoundClip} |
| 68 | + * @throws NullPointerException if {@code file} is {@code null} |
| 69 | + * @throws IOException if there is an error reading from {@code file} |
| 70 | + */ |
| 71 | + public static SoundClip createSoundClip(File file) throws IOException { |
| 72 | + if (file == null) { |
| 73 | + throw new NullPointerException("file must not be null"); |
| 74 | + } |
| 75 | + return new SoundClip(file); |
| 76 | + } |
| 77 | + |
| 78 | + private SoundClip(File file) throws IOException { |
| 79 | + this.clip = JavaSoundAudioClip.create(file); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@return whether this is a playable sound clip} |
| 84 | + * <p> |
| 85 | + * A value of {@code false} means that calling any of the other methods |
| 86 | + * of this class is a no-op. |
| 87 | + */ |
| 88 | + public boolean canPlay() { |
| 89 | + return clip.canPlay(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@return whether sound is currently playing} |
| 94 | + */ |
| 95 | + public boolean isPlaying() { |
| 96 | + return clip.isPlaying(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Starts playing this sound clip. |
| 101 | + * Each time this method is called, the clip is restarted from the beginning. |
| 102 | + * This method will return immediately whether or not sound is played, |
| 103 | + * and possibly before the sound has started playing. |
| 104 | + * <p> |
| 105 | + * Threading notes : Most applications will not need to do anything except call {@code play()}. |
| 106 | + * The following is therefore something most applications need not be concerned about. |
| 107 | + * Play back is managed in a background thread, which is usually a daemon thread. |
| 108 | + * Running daemon threads do not prevent the VM from exiting. |
| 109 | + * So at least one thread must be alive to prevent the VM from terminating. |
| 110 | + * A UI application with any window displayed automatically satisfies this requirement. |
| 111 | + * Conversely, if the application wants to guarantee VM exit before the play() has completed, |
| 112 | + * it should call the {@code stop()} method. |
| 113 | + */ |
| 114 | + public void play() { |
| 115 | + clip.play(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Starts playing this sound clip in a loop. |
| 120 | + * Each time this method is called, the clip is restarted from the beginning. |
| 121 | + * This method will return immediately whether or not sound is played, |
| 122 | + * and possibly before the sound has started playing. |
| 123 | + * <p> |
| 124 | + * Threading notes : Most applications will not need to do anything except call {@code loop()}. |
| 125 | + * The following is therefore something most applications need not be concerned about. |
| 126 | + * Play back is managed in a background thread, which is ususally a daemon thread. |
| 127 | + * Running daemon threads do not prevent the VM from exiting. |
| 128 | + * So at least one thread must be alive to prevent the VM from terminating. |
| 129 | + * A UI application with any window displayed automatically satisfies this requirement. |
| 130 | + * Conversely, if the application wants to guarantee VM exit before the play() has completed, |
| 131 | + * it should call the {@code stop()} method. |
| 132 | + */ |
| 133 | + public void loop() { |
| 134 | + clip.loop(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Stops playing this sound clip. |
| 139 | + * Call this if the clip is playing and the application needs to stop |
| 140 | + * it early, for example so that the application can ensure the clip |
| 141 | + * playing does not block exit. It is also required to stop a {@code loop()}. |
| 142 | + */ |
| 143 | + public void stop() { |
| 144 | + clip.stop(); |
| 145 | + } |
| 146 | +} |
0 commit comments