How To Play MP3 Files in Java Eclipse

How is it going coders? for today Tutor… I’m gonna tell y’all how to Play MP3 files in Java Program. Just like always for todays example I’m using Eclipse, you could use it or any other IDE that suites you. For todays program I’m using MP3 files, so thats not too hard to find.

 First of all you need to have your MP3 files inside your source folder like this :

Just look at that “Mfg” MP3 files that I have inside my source folder for “MainMP3” Program.
Now for the coding Spices :


Wait!!! Before all of that You need to Download JLayer Here :  JLayer
And add it to your program Build Path using “Add External JARs” :

//1st now well import the important classes
//(notice that there is a class/package named javazoom.jl…)

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

//2nd we create our method inside our class
public class MainMP2 {
 

//here we create the main method

    public static void main(String[] args){
       
        try{
        FileInputStream fileInputStream = new FileInputStream(“Mfg.mp3”);
 //here we find our sound file
        Player player = new Player(fileInputStream);
        player.play();
        System.out.println(“Song is Playing”);
        } catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(JavaLayerException e){
            e.printStackTrace();
        }
       
    }
   
}

Well since I can’t tell you how it sounds in here, you can go and check my Youtube Channel for the Voice explanation : MP3 Files in Java Eclipse Check it out!! and subscribe

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