Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Playing MP3 Files in JavaFX

DZone's Guide to

Playing MP3 Files in JavaFX

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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

 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}