9

Sadly MP3 support within Java is lacking. I am developing an app that needs to receive chunks of MP3 and play them. I was using Jlayer MP3 library like this:

import javazoom.jl.player.Player;
public class MP3 {
    private String filename;
    private Player player; 

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }

    public void close() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        player.play();
}

But my problem is that I have only chunks of the full MP3 file, and I need to play them as they arrive. Is there any better alternative?

Edit

Found an interesting similar question: MP3 won't stream with JMF Also: Decoding MP3 files with JLayer

Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • You should look at this thread , already solved a problem fallowing this simple steps http://stackoverflow.com/questions/14410344/jersey-rest-support-resume-media-streaming/14476402#14476402 – GhostDerfel Feb 10 '14 at 14:16
  • @GhostDerfel: I think that works only within browsers?... I need to play that from a desktop app – dynamic Feb 10 '14 at 14:19
  • 3
    Mmmm you are right, never done that for desktop app...only web and Android , sorry :(. If I find something I will post here as reference – GhostDerfel Feb 10 '14 at 14:21
  • 4
    I think this one worth the try https://github.com/Hotware/HotSound – GhostDerfel Feb 10 '14 at 14:24
  • Have you checked: http://stackoverflow.com/questions/9818155/stream-music-with-java? – eduardohl Feb 16 '14 at 16:00
  • I need to manual manage the stream. Can't use http – dynamic Feb 16 '14 at 17:25
  • What exactly do you mean with chunks? Is each chunk it's "own" MP3 file (with Header/Tags/etc.) or just a part of the whole file which appended in sequence results in the original file? – AleX Feb 18 '14 at 02:26
  • Hotsound Example I think may help: https://github.com/Hotware/HotSoundExamples/blob/master/src/de/hotware/hotsound/examples/PlaylistPlayer.java – Durandal Feb 19 '14 at 07:21
  • @dynamic Did you find a working solution? If so, I would really appreciate it if you could share the solution as I'm working to do the same thing(stream mp3 files in chunks) – xabush Jan 23 '16 at 11:57

1 Answers1

1

Create a class that implements InputStream, and works to receive the chunks as they arrive, but serves up the bytes to the player. Just make sure to track where you are in each chunk as it requests bytes and then discard the chunk when you've burned through it and start serving up data from the next one.

Since the player expects to deal with an InputStream, it will be none the wiser. You probably won't need to wrap that in a BufferedInputStream since you'll be handling that in your class.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
  • can you please add a code demonstration to your answer? – xabush Feb 02 '16 at 19:12
  • @avidProgrammer Not without more information about how the data is being served. – BillRobertson42 Feb 02 '16 at 19:15
  • In my case, the data is continuously written to a temporary file and the player plays from that file as an inputstream. Here is a question I asked about the same problem http://stackoverflow.com/questions/35157247/playing-streamed-mp3-data-repeatedly-with-jlayer – xabush Feb 02 '16 at 19:54