How to play mp3 files in java using eclipse?

Recommended Answers

All 6 Replies

A few minutes on google and here you go...

import javax.sound.sampled.AudioInputStream;
   import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;

       try{
      AudioInputStream audioInputStream =AudioSystem.getAudioInputStream(this.getClass().getResource("path of relative sound file in src folder"));
     Clip clip = AudioSystem.getClip();
     clip.open(audioInputStream);
     clip.start( );
    }
   catch(Exception ex)
   {  }

Next time try to write your own code and if you came on problem then ask for help, I see that this is your first post so next time I want to se efford...

Hi!

By using JMF you can play easily, and before you should install the JMF in your System..,

import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;

class MP3Play

{
 public static void main(String args[]) throws Exception

 {
//File Path
 File f=new File(args[0]);
 // Create a Player object that realizes the audio
 final Player p=Manager.createRealizedPlayer(f.toURI().toURL());

  p.start();
  Scanner s=new Scanner(System.in);
  String st=s.nextLine();
   // If user types 'stop', stop the audio

   if(st.equals("stop"))

   {
   p.stop();

   }
 }

}

Hi milil
Please don't EVER do this in code that a learner may read:

catch(Exception ex)
{ }

all you are doing is guaranteeing that when it goes wrong they will have no idea why. Always start with an ex.printStackTrace();

and don't immediately jump to recommending JMF.
It is quite old, no longer maintained/supported, and a mess, frankly saying.

a simple import of the JavaZooms JLayer library would be a lot better.

as for the OP: the first thing to do, is to realize that this is not eclipse-related. whether you write your code in netbeans, eclipse, or notepad, the work stays the same.

Now Java 8 is out you should look at JavaFX (the replacement for Swing) which includes comprehensive media playing and is a fully-supported mainstream part of the Oracle Java API.

http://docs.oracle.com/javafx/2/media/overview.htm

... further to the JavaFX suggestion...
just for fun I tried it. It's really easy, apart from one gotcha that took a little research. Anyway, here's the result

You create a JavaFX MediaPlayer, passing it the URI of the mp3 file, and call its play() method. The following code gets the URI string from an ordinary file path/name string...

String uriString = new File(fileName).toURI().toString();

then this creates the player

MediaPlayer player = new MediaPlayer(new Media(uriString));
player.play(); // or stop() or pause() etc etc

yes, it's really that easy, and is 100% mainstream Oracle-supported Java.

Now the "gotcha". If you run this as part of a non-JavaFX application then the JavaFX environment won't have been initialised properly. However, you can force JavaFX to initialise as part of an ordinary application by simply creating a Swing JavaFX panel, as in

new javafx.embed.swing.JFXPanel(); // forces JavaFX init

so that's it. In summary, a trivial player looks like:

void playMP3(String fileName) {
    new javafx.embed.swing.JFXPanel();
    String uriString = new File(fileName).toURI().toString();
    new MediaPlayer(new Media(uriString)).play();
}

... plus the imports of course.

import java.io.File;
import javafx.scene.media.MediaPlayer;

Best of all it's fully supported and is included in the standard Java SE JRE from Java SE 7 update 6 onwards.

commented: nice. might take a look myself whenever I write me a new one. +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.