Open links in new tab
  1. You can create an audio player in Java using the built-in javax.sound.sampled package without relying on external libraries. This approach supports formats like WAV, AIFF, AU, AIFC, and SND.

    Steps to Implement

    1. Load the Audio File Use AudioSystem.getAudioInputStream(File file) to convert the audio file into a stream.

    2. Create and Open a Clip Obtain a Clip instance from AudioSystem.getClip() and open it with the audio stream.

    3. Control Playback Implement methods for play, pause, resume, restart, stop, and jump to a specific position using clip methods like start(), stop(), setMicrosecondPosition().

    4. Reset Audio Stream When Needed If you want to replay or resume from a paused state, reset the audio stream before starting again.

    Example Code

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    import javax.sound.sampled.*;

    public class SimpleAudioPlayer {
    Long currentFrame;
    Clip clip;
    String status;
    AudioInputStream audioInputStream;
    static String filePath;

    public SimpleAudioPlayer() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
    clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    public void play() { clip.start(); status = "play"; }
    public void pause() { currentFrame = clip.getMicrosecondPosition(); clip.stop(); status = "paused"; }
    public void resumeAudio() throws Exception { resetAudioStream(); clip.setMicrosecondPosition(currentFrame); play(); }
    public void restart() throws Exception { resetAudioStream(); currentFrame = 0L; play(); }
    public void stop() throws Exception { currentFrame = 0L; clip.stop(); clip.close(); }
    public void jump(long c) throws Exception { if (c > 0 && c < clip.getMicrosecondLength()) { resetAudioStream(); currentFrame = c; clip.setMicrosecondPosition(c); play(); } }

    public void resetAudioStream() throws Exception {
    audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
    clip.open(audioInputStream);
    clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    public static void main(String[] args) {
    try {
    filePath = "path/to/audio.wav";
    SimpleAudioPlayer player = new SimpleAudioPlayer();
    player.play();
    Scanner sc = new Scanner(System.in);
    while (true) {
    System.out.println("1. pause 2. resume 3. restart 4. stop 5. jump");
    int choice = sc.nextInt();
    if (choice == 1) player.pause();
    else if (choice == 2) player.resumeAudio();
    else if (choice == 3) player.restart();
    else if (choice == 4) { player.stop(); break; }
    else if (choice == 5) { System.out.println("Enter time in microseconds:"); player.jump(sc.nextLong()); }
    }
    sc.close();
    } catch (Exception e) { e.printStackTrace(); }
    }
    }
    Copied!
  1. How to play an Audio file using Java - GeeksforGeeks

    Jul 3, 2022 · In this article we will see, how can we play an audio file in pure java, here pure means, we are not going to use any external library. You can create …

  2. People also ask
  3. audio - Reading wav file in Java - Stack Overflow

    How can I read wav files in Java and assign them into an array or something like that (you can suggest ideas for it) to classify them? EDIT: I want to use APIs for reading wav files and for K-means. I think …

    • Reviews: 1

      Code sample

      AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(new File(response.get()));
      assertEquals(1, audioFileFormat.getFormat().getChannels());
      assertEquals(8000.0, audioFileFormat.getFormat().getSampleRate(), 0.0f);
      assertEquals(8, audioFileFormat.getFormat().getSampleSizeInBits());
    • How to Play Sound With Java - Baeldung

      In this tutorial, we’ll learn how to play sound with Java. The Java Sound APIs are designed to play sounds smoothly and continuously, even very long sounds. As part of this tutorial, we’ll play an audio file using Clip and SourceDataLineSound APIs provided by Java. We’ll also play different audio format files. In addition, we’ll discuss the pros an...
      See more on baeldung.com
      • Reviews: 2
      • Published: May 28, 2022
    • What Are the Best Java Libraries for Working with MP3 and Audio Files ...

      Discover the top Java libraries for MP3 and audio processing. Explore their features, benefits, and how to integrate them into your projects.

    • Java audio processing: Playing and recording sound - Reintech

      Apr 18, 2023 · Learn how to play and record audio files using the Java Sound API in this comprehensive tutorial for software developers.

    • How to Play Audio File (.WAV) Using Java Applet?

      Jul 23, 2025 · MusicPlayerApplet class inherits the property of Applet in Java. The interface consists of the input text field to enter a filename with an extension of …

    • AudioFileReader (Java Platform SE 8 ) - Oracle

      Provider for audio file reading services. Classes providing concrete implementations can parse the format information from one or more types of audio file, and can produce audio input streams from …

    • AudioFileReader (Java SE 26 & JDK 26 [build 1])

      Provider for audio file reading services. Classes providing concrete implementations can parse the format information from one or more types of audio file, and can produce audio input streams from …

    • Mastering Java Sound – A Comprehensive Guide to Playing Sound in Java

      To load audio files in Java, you can utilize the AudioInputStream class, which acts as a bridge between audio file data and the Java Sound API. The AudioInputStream class provides methods for reading …