Java Music Player: Song information and playing


Question

In Android, we can use the mediaplayer to play songs on the device, and cursors to get the track information (which the OS keeps track of). My question is, what are the Java equivalents of these?

Note: I have seen people mentioning JavaFX, however this does not seem to be installed with the JVM by default and thus my program would no longer be cross-platform.

Any suggestions?

Note: These will all be local files, though they may be of any audio filetype.

1
2
6/11/2013 2:43:57 AM

Accepted Answer

I'll provide information on a JavaFX related solution for a Java music player as I think it is a good technology to use for this task.

Supported Platforms and Formats

they may be of any audio filetype.

Supported audio types for JavaFX are documented in the javafx.scene.media javadoc. Supported formats for JavaFX 2.2 are MP3, AAC and PCM in various containers such as mp3, mp4 and wav files. Other formats such as Ogg Theora are not supported - so if you need such formats you will need a different solution than JavaFX 2.2.

I have seen people mentioning JavaFX, however this does not seem to be installed with the JVM by default and thus my program would no longer be cross-platform

JavaFX is installed with the Oracle Java runtime by default since Java 7 update 6 and JavaFX 2.2 runs on OS X, Windows and Linux. There are currently no widely available solutions for running JavaFX on mobile platforms (though that may change in the future).

Further information on using JavaFX with Java 7 is in my answer to Compiling and running with JavaFX 2.1 as well as the JavaFX Deployment Guide.

get the track information (which the OS keeps track of)

Most track information is actually encoded in media files rather than in an OS database. You can retrieve metadata from media files played by JavaFX. Parsing of a limited amount of metadata from media files is supported as defined in the media documentation. Note that not all media contains all metadata. I tested a free mp3 file from amazon and was able to extract plenty of metadata from it successfully. Metadata from a test aac encoded m4a file from iTunes was mostly empty. So results depend on the media file being used.

Sample Player

I created a sample music player in JavaFX which plays audio files from a directory, displaying metadata for each song.

audioplayer

java2s also host source for a media player created by the authors of the Pro JavaFX 2 book. That might be a better reference than my code, as mine was a quick hack, whereas the pro source is from a professionally published book.

Additional Questions

Any suggestions for those of us on linux not using Oracle's JRE?

Oracle JRE is available on Linux, but if you don't want to use that, you can build openjfx for openjdk and use that, or you could develop your app using Oracle JRE and use the JavaFX packaging tools to distribute it as a self-contained application so it has no external JRE dependency.

7
5/23/2017 10:32:46 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon