Sergey has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

Playing MP3 Files in JavaFX

02.27.2008
| 12182 views |
  • submit to reddit

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

 

0
Your rating: None
Published at DZone with permission of its author, Sergey Surikov.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Thomas Mueller replied on Wed, 2008/02/27 - 11:39am

A bit simpler than JLayer is mp3transform at http://code.google.com/p/mp3transform

It's only about 10 classes. 

(disclaimer: I wrote it, and it is based on JLayer)

Andres Almiray replied on Wed, 2008/02/27 - 12:05pm

I'm still waiting to see the WOW factor. Some shiny eye-catching controls would have been nice, I mean the example is really a regular Java class sans Swing (actually Swing driven by JavaFX).

Umberto Zappia replied on Wed, 2008/02/27 - 12:18pm

There are so many examples of what JavaFX can do right now, and they are pretty neat, simple and still in beta, I can't wait to see JavaFX in action @ JavaOne.

Great post.

 

Sergey Surikov replied on Wed, 2008/02/27 - 4:33pm in response to: Andres Almiray

[quote]Some shiny eye-catching controls would have been nice[/quote]

Look to chart controls from

http://molgav.nn.ru/index.php?option=com_content&view=article&id=241%3A--&Itemid=53

isn't they nice?

 

 

Andres Almiray replied on Wed, 2008/02/27 - 5:13pm in response to: Sergey Surikov

Much better but nothing out of the ordinary (after all we can do that with Swing today). What I mean is if JavaFX is a great step into eye-catching UIs and ease-of-use I have yet to see *any* example that makes me shout "OMG I can't believe how easy is to do _that_ with JavaFX, I can't do that with Swing!".

Needless to say I find interesting your latest JavaFX postings but again I can do that with Swing today without breaking a sweat.

Carl Antaki replied on Wed, 2008/02/27 - 9:24pm

The last time I checked Javazoom which has not changed since, it didn't play some of the MP3s I have. I mean Sun should probably need to build their own or improve Javazoom instead of using this library that doesn't work all the time.

Java has unfortunately lost the game on the browser RIA as Flash is the standard. Sun cannot match Adobe's tools. The new Java plugin that will be released should have been done years ago, Flash wouldn't have existed maybe. Don't get me wrong I love Swing but I really didn't like JavaFX syntax. Also I've bashed over and over over having context menus (Cut Copy Paste, Undo and Redo) in text components but Sun doesn't seem to understand that it kills accessibility.

 

Brian Matzon replied on Thu, 2008/02/28 - 2:56am

Just dont use mp3! ogg vorbis is free, whereas mp3 requires a license agreement: http://mp3licensing.com/royalty/software.html

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.