views:

45

answers:

2

Hi, my goal is to play an mp3 file from Java. With every approach that I took, it always fails on a LineUnavailableException.

    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new URL("http://localhost:8080/agriserver/facebook/sound/test6.mp3"));
    Clip clip = AudioSystem.getClip(info);
    clip.open(inputStream);
    clip.start();

Failed attempts to fix it:

  • Use Sun's mp3 plugin.
  • Use Jlayer 3rd party library
  • Use Tritonus 3rd party library
  • Re-encode the mp3 with Sony Sound Forge, Adobe Sound Booth, all no luck
  • Re-encode the mp3 with different encode rates and sampling rates
  • Try to use JMF
  • Use random mp3 from the Internet that plays fine in other applications
  • Read postings with the same error. None of the postings have an answer that helped resolve the issue.

Here is the exception: Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, not supported. at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:494) at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1280) at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107) at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1061) at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1151) at Demo.playMp3(Demo.java:83)

A: 

OK - Let's start by ruling out your MP3 files and your code.

  1. Pick an MP3 file that you have and play it with any MP3 player.
  2. Download http://www.javazoom.net/javalayer/sources/jlayer1.0.1.zip
  3. Extract jl1.0.1.jar from zip file and put in your classpath
  4. Cut and Paste the code at the end of this answer into your dev environment.
  5. compile and run making sure your mp3 file in step 1 is the parameter to the file. (In my case I had this "C:\\Users\\romain\\Music\\Al DiMeola\\Elegant Gypsy\\01 Flight over Rio Al DiMeola.mp3")
  6. I tested this and it works fine.

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    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);
        }
    
    
    
    // run in new thread to play in background
    new Thread() {
        public void run() {
            try { player.play(); }
            catch (Exception e) { System.out.println(e); }
        }
    }.start();
    
    } // test client public static void main(String[] args) { String filename = args[0]; MP3 mp3 = new MP3(filename); mp3.play(); }

    }

Romain Hippeau