How to play an mp3 file in java

2020-02-12 11:09发布

问题:

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.

回答1:

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



回答2:

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;


回答3:

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 {
    }
}


标签: java audio mp3