3 Years
Discussion Span
Last Post by JamesCherrill

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

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

      AudioInputStream audioInputStream =AudioSystem.getAudioInputStream(this.getClass().getResource("path of relative sound file in src folder"));
     Clip clip = AudioSystem.getClip();
     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...



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());

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





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.


... 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.

Edited by JamesCherrill

Votes + Comments
nice. might take a look myself whenever I write me a new one.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.