How to add audio on JSwing in Java

In this tutorial, we will learn how to add audio on JSwing in Java. We can add audio to our JSwing in many ways. In this tutorial, we will do it in an easy way i.e.by making a class for the whole audio system resource to work. Moreover, we will add two buttons for the audio to play and to stop using that class.

Let’s learn

The very first thing is that we will need to convert the music or the audio if it is an MP3 file or any other file to a wave file by a suitable converter.

The package is included for the audio system to work with the other packages is:

import javax.sound.sampled.*;

This package includes AudioSystem, AudioInputStream, Clip, etc for audio system resources to run. After this, we will create a class that enables the working of the audio system resources, and then this class is called by the two buttons created. These buttons use the class for the audio system to work i.e. either it is played or stopped.

In the class, we will create an object of AudioInputStream using AudioSystem.getAudioInputStream(file) from a given sound file. AudioInputStream converts an audio file into the stream. Then we will get the reference object of the clip from the Audiosystem.

Finally, we will obtain a clip, open it and start playing the audio.

The open() method, helps in reading the audio data.

Class Music:

public class Music {
    Clip clip;
    AudioInputStream sound;

    public void setFile(String soundFileName) {
        try {
            File file = new File(soundFileName);
            sound = AudioSystem.getAudioInputStream(file);
            clip = AudioSystem.getClip();
            clip.open(sound);
        } catch (Exception e) {

        }
    }

    public void play() {
        clip.start();
    }

    public void stop() throws IOException {
        sound.close();
        clip.close();
        clip.stop();
    }
}

The method play() plays the audio system and the method stop() stops the audio system by releasing the resources acquired.

Play Java Audio – JSwing

Complete code:

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Myframe {
    JFrame frame;
    JPanel panel;
    JButton button1, button2;
    String sound_track;
    Music se = new Music();

    public static void main(String args[]) {
        new Myframe();
    }

    public class Music {
        Clip clip;
        AudioInputStream sound;

        public void setFile(String soundFileName) {
            try {
                File file = new File(soundFileName);
                sound = AudioSystem.getAudioInputStream(file);
                clip = AudioSystem.getClip();
                clip.open(sound);
            } catch (Exception e) {

            }
        }

        public void play() {
            clip.start();
        }

        public void stop() throws IOException {
            sound.close();
            clip.close();
            clip.stop();
        }
    }

    public Myframe() {
        sound_track = "Believe - NEFFEX.wav";
        frame = new JFrame("Audio");
        frame.setSize(700, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        frame.add(panel);

        frame.setVisible(true);

        button1 = new JButton("Start");
        button1.setBounds(200, 240, 100, 30);
        button1.setBackground(Color.WHITE);
        button1.setFocusPainted(false);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                se.setFile(sound_track);
                se.play();
            }
        });
        panel.add(button1);

        button2 = new JButton("Stop");
        button2.setBounds(400, 240, 100, 30);
        button2.setBackground(Color.WHITE);
        button2.setFocusPainted(true);
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                se.setFile(null);
                try {
                    se.stop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        panel.add(button2);
    }
}

If you run this program you will be able to add audio to your JSwing in Java.

Output:

Start                       Stop

 

 

 

The window after running the above code looks like this:

How to add audio on JSwing in Java

On clicking the button named “Start”, the audio starts playing, and on clicking the next button named “Stop”,  the audio stops.

Hope this tutorial was useful.

 

One response to “How to add audio on JSwing in Java”

  1. rodin says:

    how to insert in file audio to database

Leave a Reply

Your email address will not be published.