4

I am trying to play a song (mp3 file) in java. I have been looking around for a few hours now and none of the ways I found worked properly.

public void play()
{
    String song = "song.mp3";
    Media track = new Media(song);
    MediaPlayer mediaPlayer = new MediaPlayer(track);
    mediaPlayer.play();
}

I have tried doing that but it gives me errors.

I have imported JMF and JLayer.

I have also read other questions that are like this one on this forum and none of them have helped me.

I just need a hand to help play an mp3 file.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
user2413200
  • 245
  • 3
  • 7
  • 12

3 Answers3

3

The easiest way I found was to download the JLayer jar file from http://www.javazoom.net/javalayer/sources.html and to add it to the Jar library http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29

Here is the code for the class

public class SimplePlayer {

public SimplePlayer(){

    try{

    FileInputStream fis = new FileInputStream("File location.");
    Player playMP3 = new Player(fis);

    playMP3.play();

    }catch(Exception e){System.out.println(e);}
}

}

and here are the imports

import javazoom.jl.player.*;
import java.io.FileInputStream;
Vlad
  • 142
  • 1
  • 3
  • 6
2

For this you'll need to install Java Media Framework (JMF) in your PC. One you have it installed,then try this piece of code:

import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
 public static void main(String args[]) throws Exception
 {


 // Take the path of the audio file from command line
 File f=new File("song.mp3");


 // Create a Player object that realizes the audio
 final Player p=Manager.createRealizedPlayer(f.toURI().toURL());


  // Start the music
  p.start();


  // Create a Scanner object for taking input from cmd
  Scanner s=new Scanner(System.in);


  // Read a line and store it in st
  String st=s.nextLine();


   // If user types 's', stop the audio
   if(st.equals("s"))
   {
   p.stop();
   }
 }
}

You may run into unable to handle formaterror, that is because Java took out the MP3 support by default (pirate copyright issue), you are required to install a “JMF MP3 plugin” in order to play MP3 file.

Go Java’s JMF website to download it http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html

To be sure that you are using a supported format file, check here:

http://www.oracle.com/technetwork/java/javase/formats-138492.html

If you are using windows7, you may have to read this as well:

https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • i tried this and it says unable to handle format then it runs out of memory – user2413200 Jun 01 '13 at 07:18
  • @user2413200 i have updated the resolution for the error occured. – Juned Ahsan Jun 01 '13 at 07:26
  • well i added mp3plugin.jar and it didn't do anything – user2413200 Jun 01 '13 at 07:38
  • I get this: Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 6000.0 frame rate, FrameSize=32768 bits Failed to realize: com.sun.media.PlaybackEngine@292740 Error: Unable to realize com.sun.media.PlaybackEngine@292740 – user2413200 Jun 01 '13 at 07:46
  • @user2413200 Check if the format is listed here: http://www.oracle.com/technetwork/java/javase/formats-138492.html – Juned Ahsan Jun 01 '13 at 07:53
  • @user2413200 Also if u are on windows7 then read this : https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45 – Juned Ahsan Jun 01 '13 at 07:53
1

How about JavaFX application-

import java.net.URL;
import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class VLC extends Application {
    void playMedia() {
        String mp3 = "00- Tu Hi Mera.mp3";
        URL resource = getClass().getResource(mp3);
        System.out.println(resource.toString());
        Media media = new Media(resource.toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    }
    public static void main(String args[]) {
        new VLC().playMedia();
    }
    @Override
    public void start(Stage stage) throws Exception {
    }
}
Varun Kumar
  • 2,543
  • 1
  • 23
  • 22