Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8356200

Need a simple way to play back a sound clip

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 25
    • client-libs
    • None
    • source
    • minimal
    • new API
    • Java API
    • SE

      Summary

      Add a new class javax.sound.SoundClip which provides simple API to play back a supported audio stream from a file.

      Problem

      Applications have used java.applet.AudioClip.getAudioClip as a simple means to play back an audio clip. However the java.applet package is deprecated for removal.

      Solution

      Since the applet AudioClip class is just a wrapper around java.sound APIs, define a new class in the javax.sound package in the java.desktop module which provides similar easy to use functionality.

      A new package javax.sound is also introduced since existing javax.sound APIs are in the packages javax.sound.midi and javax.sound.sampled.

      Specification

      new package javax.sound:

      +/**
      + * Provides interfaces and classes for the Java Sound API.
      + * The API is divided into sub-packages.
      + * <ul>
      + * <li>Capture, processing and playback of sampled audio data is under {@link javax.sound.sampled}.
      + * <li>Sequencing, and synthesis of MIDI (Musical Instrument Digital Interface) data is under {@link javax.sound.midi}.
      + * </ul>
      + *
      + * <h2>Related Documentation</h2>
      + * For more information on using Java Sound see:
      + *   <a href="https://docs.oracle.com/javase/tutorial/sound/">
      + *   The Java Sound Tutorial</a>
      + *
      + * @since 25
      + */
      +package javax.sound;

      ============================

      new class javax.sound.SoundClip:

      +package javax.sound;
      +
      +/**
      + * The {@code SoundClip} class is a simple abstraction for playing a sound clip.
      + * It will play any format that is recognized by the {@code javax.sound} API,
      + * and for which it has support. This includes midi data.
      + * Playing sound requires that the environment grants access to audio devices.
      + * Typically this means running the application in a desktop environment.
      + * <p>
      + * Multiple {@code SoundClip} items can be playing at the same time, and
      + * the resulting sound is mixed together to produce a composite.
      + *
      + * @since 25
      + */
      +public final class SoundClip {
      +
      +    /**
      +     * Creates a {@code SoundClip} instance which will play a clip from the supplied file.
      +     * <p>
      +     * If the file does not contain recognizable and supported sound data, or
      +     * if the implementation does not find a suitable output device for the data,
      +     * playing the clip will be a no-op.
      +     *
      +     * @param file the file from which to obtain the sound data
      +     * @return a {@code SoundClip}
      +     * @throws IllegalArgumentException if {@code file} is {@code null}
      +     * @throws IOException if there is an error reading from {@code file}
      +     */
      +    public static SoundClip createSoundClip(File file) throws IOException;
      +
      +    /**
      +     * {@return whether this is a playable sound clip}
      +     * <p>
      +     * A value of {@code false} means that calling any of the other methods
      +     * of this class is a no-op.
      +     */
      +    public synchronized boolean canPlay();
      +
      +    /**
      +     * {@return whether sound is currently playing}
      +     */
      +    public synchronized boolean isPlaying();
      +
      +    /**
      +     * Starts playing this sound clip.
      +     * Each time this method is called, the clip is restarted from the beginning.
      +     */
      +    public synchronized void play();   
      +
      +    /**
      +     * Starts playing this sound clip in a loop.
      +     * Each time this method is called, the clip is restarted from the beginning.
      +     */
      +    public synchronized void loop();    
      +
      +    /**
      +     * Stops playing this sound clip.
      +     */
      +    public synchronized void stop();

            prr Philip Race
            prr Philip Race
            Alexey Ivanov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: