How to add Sound / Music in Java Program

How is it going coders? for today specials… I’m goinna tell you on how to add sound or music into Java Program. For todays example I’m using Eclipse, you could use it or any other IDE that suites you. First of all the sound file I’m using is a wav format, keep that in mind.

Okay so first of all you need to have is a sound file, and you need to put it inside the project folder as shown below :

So I have a sound file named “arrow” inside my project folder named “BackGroundSound”.
After that, we’ll just have to do a little bit of coding :


//1st now well import the important classes 
//(notice that there is a class/package named sun.audio)
import sun.audio.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.*;

//2nd we create our method inside our class

public class BackSound extends JFrame{

//here we create the main method

    public static void main(String[] args){
      JFrame frame = new JFrame(“BackSound”);   
      frame.setSize(300, 300);
      JButton button = new JButton(“Turn On Sound”); //we create the buttons to click
      frame.add(button);
      button.addActionListener(new AL());
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }
   
    public static class AL implements ActionListener{
        public final void actionPerformed(ActionEvent e){
        //here we creates a method AL that implements ActionListener for the button
            music();
        }}
   
    public static void music(){
        AudioPlayer MGP = AudioPlayer.player; //here we create an AudioPlayer from sun.audio
        AudioStream BGM; //here we create an AudioStream from sun.audio
        AudioData MD; //here we create an AudioData from sun.audio
       
        ContinuousAudioDataStream loop = null;
        //here we loop our audio inside the program
       
        try{
        BGM = new AudioStream(new FileInputStream(“arrow.wav”)); //here we find our sound file
        MD = BGM.getData();
        loop = new ContinuousAudioDataStream(MD);
        }catch(IOException error){//here we catch an IOException, if it occurs it will print “yo”
            System.out.println(“yo”);
        }
       
        MGP.start(loop); //here we’ll start the loop whenever the method “music” is called
    }
}

Well since I can’t tell you how it sounds in here, you can go and check my Youtube Channel for the Voice explanation : How to add Sound / Music in Java Program (Eclipse) Check it out!! and subscribe

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

4 thoughts on “How to add Sound / Music in Java Program”

  1. Hey Rafif Santika,

    Thanks for your Tutorial on how to implement Audio in Eclipse.

    I struggle, cause I wanna only replay a sound file ONCE, when command line music() is hit in another eclipse project. So for this, I create a Audio Method as you shown, BUT: can I access the method by another running java project by insert music() in the other? And how can I replace the continuous audio data stream code for a single playback with no loop of the sound? And for some command lines, i get error: no access to JRE (on other Windows User Account with no read permission probably, this works for my other projects, but here its not. Can I just copy the directory of JRE on the other Useraccount or do i need to re-install it on the active User Win Account? Thanks for your help! An Email Reply would be amazing!
    Best Regards,
    Lukas

    1. Hi Lukas,

      Thank you for asking.

      First, if you want to do a single playback of the sound without any loop just replace *ContinuousAudioDataStream* code to *AudioDataStream*.

      For the access to JRE, try to reset your Java runtime in the project properties.

  2. Thanks for your reply, it helped, so far everything looks good, except that i get a “yo” ^^ i tested, and the exception occurs in the very last few lines (starting with try{..)…

    My adaption of your example looks like this by now:
    //Musikplay

    //1st now well import the important classes
    //(notice that there is a class/package named sun.audio)
    import sun.audio.*;

    //import java.awt.event.ActionEvent;
    //import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.IOException;

    //import javax.swing.*;

    //2nd we create our method inside our class
    public class BackSoundIntegrated //extends JFrame
    {

    //here we create the main method
    // public static void main(String[] args){
    // JFrame frame = new JFrame(“BackSound”);
    // frame.setSize(300, 300);
    // JButton button = new JButton(“Turn On Sound”); //we create the buttons to click
    //frame.add(button);
    //button.addActionListener(new AL());
    //frame.setVisible(true);
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // }

    // public static class AL implements ActionListener{
    // public final void actionPerformed(ActionEvent e){
    //here we creates a method AL that implements ActionListener for the button
    // music();
    // }}

    public static void music(){

    AudioPlayer MGP = AudioPlayer.player; //here we create an AudioPlayer from sun.audio
    AudioStream BGM; //here we create an AudioStream from sun.audio
    AudioData MD; //here we create an AudioData from sun.audio

    AudioDataStream loop = null;
    //here we loop our audio inside the programm

    try{
    BGM = new AudioStream(new FileInputStream(“AlienSiren.wav”)); //here we find our sound file
    MD = BGM.getData();
    loop = new AudioDataStream(MD);

    }catch(IOException error){//here we catch an IOException, if it occurs it will print “yo”
    System.out.println(“yo”);
    }

    MGP.start(loop); //here we’ll start the loop whenever the method “music” is called
    }
    }

    Any Idea how to make it run??? Thanks for your help in advance!!

    Best,

    Lukas

    1. Lukas, I have tried your code.. the “yo” is thrown whenever an IOexception has occurred. It means that the process of input or output has failed to work. Now, have you put your sound file/source file into the project folder?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.