Skip to content
Related Articles

Related Articles

Java Sound API

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 17 May, 2022

JavaSound is a collection of classes and interfaces for effecting and controlling sound media in java. It consists of two packages.

  • javax.sound.sampled: This package provides an interface for the capture, mixing digital audio.
  • javax.sound.midi: This package provides an interface for MIDI (Musical Instrument Digital Interface) synthesis, sequencing, and event transport.

What is MIDI?

We will be exploring MIDI more about MIDI here. So MIDI is like a sheet of notes that the midi capable instrument can play, think of it as an HTML document and midi capable instrument as a web browser. MIDI file has information about how a song should be played, just like an instruction sheet for a guitar player. MIDI devices know how to read a midi file and generate sound.

In order to make actual sound we need four things for this:

  1. Instrument (that plays the music) SEQUENCER
  2. Song (the music to be played)  SEQUENCE
  3. Track (which holds the notes)
  4. Notes (the actual music information)  MIDI EVENTS

JavaSound API got all of these covered.



Step 1: Get a Sequencer and open it

// Make a sequencer named player and open it
Sequencer player = MIDISystem.getSequencer();;

Step 2: Make a new Sequence

// Make a new sequence 
Sequence seq = new Sequence(Sequence.PPQ, 4);

Step 3: Get a new Track from the Sequence

// Creating new Track
Track t = seq.createTrack();

Step 4: Fill the Track with MIDIEVENTS  

// Filling the Track with MidiEvent and
// giving the Sequence to the Sequencer


// Play it using start




// Java Program to Illustrate JAva Sound API
// Importing classes from
// javax.sound package
import javax.sound.midi.*;
// Main class
// MiniMusicApp
public class GFG {
    // Method 1
    // Main driver method
    public static void main(String[] args)
        // Creating object of class inside main()
        GFG minimusic = new GFG();
        // Calling method 2 to play the sound;
        // Display message on the console for
        // successful execution of program
            "Successfully compiled and executed");
    // Method 2
    // To play the sound
    public void play()
        // Try block to check for exceptions
        try {
            // Getting a sequencer and open it
            Sequencer player = MidiSystem.getSequencer();
            // Making 1a new Sequence
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            // Creating a new track
            Track track = seq.createTrack();
            // Making a Message
            ShortMessage a = new ShortMessage();
            // Put the Instruction in the Message
            a.setMessage(144, 1, 44, 100);
            // Make a new MidiEvent
            MidiEvent noteOn = new MidiEvent(a, 1);
            // Add MidiEvent to the Track
            ShortMessage b = new ShortMessage();
            b.setMessage(128, 1, 44, 100);
            MidiEvent noteOff = new MidiEvent(b, 16);
            // Giving sequence to Sequencer
            // Start the Sequencer using start() method
        // Catch block to handle exceptions
        catch (Exception ex) {
            // Display the exception on console
            // along with line number


Successfully compiled and executed

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!