Over a million developers have joined DZone.

Playing MP3 Files in JavaFX

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

This example shows you how to modify the code in an MP3 file. You need to add several new functions to enable the application to read MP3 tags. Then, you should add the resulting JAR to the references of a NetBeans project and create a Java FX form to play the file.

Among several existing libraries for playing MP3 files, the most popular library is JLayer http://www.javazoom.net/javalayer/javalayer.html.

 

This small application plays an MP3 file in loop.

Source

package javafxmp3player;

import javafx.ui.*;
import embeddedmp3player.MP3Player;
import embeddedmp3player.MP3File;
import embeddedmp3player.IDone;

var mp3="G:\\mz\\slade\\1\\07-Slade In Flame (1974)\\03-So Far So Good.mp3";
var f = new MP3File(mp3);
var doneCather=new IDone() {
operation done() {
f.play(this);
}
};
f.play(doneCather);

Frame {
visible: true
screenx: 50
screeny: 100
width: 300
height: 300
title: mp3
onClose: operation(){
MP3Player.stop();
}
content: BorderPanel {
center: TextArea {
text: "size: {f.fileSize/1000} kb\ntitle: {f.title}
\nalbum: {f.album}\nartist: {f.artist}\nyear: {f.year}"
}
}
}

Compiled library

jmp3.zip - 100kb

 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}