How to play mp3 file in java

I am trying to play a song (mp3 file) in java. I look around for several hours, and none of the ways I found worked correctly.

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

I tried this, but it gives me errors.

I have imported JMF and JLayer .

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

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

+4
source share
3 answers

To do this, you need to install the Java Media Framework (JMF) on your computer. You have one of them 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 encounter an inability to handle formaterror because Java has pulled out MP3 support by default (a copyright piracy issue), you need to install the "JMF MP3 plugin" to play the MP3 file.

Go to the Javas Javas website to download it http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html

To verify that you are using a file in a supported format, check here:

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

If you are using Windows 7, you may also need to read this:

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

+2
source

The easiest way I've found is to download the JLayer jar file from http://www.javazoom.net/javalayer/sources.html and 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 is the import

 import javazoom.jl.player.*; import java.io.FileInputStream; 
+1
source

How about a 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 { } } 
0
source

Source: https://habr.com/ru/post/1483920/


All Articles