Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

plugins - LineUnavailableException for playing mp3 with java

My goal is to play an mp3 file from Java. With every approach that I took, it always fails with 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Apparently, the mp3 has to be read into one stream. That stream has to be read into a second stream to decode it. The below code worked:

        // read the  file
        AudioInputStream rawInput = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data));

        // decode mp3
        AudioFormat baseFormat = rawInput.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED, // Encoding to use
            baseFormat.getSampleRate(),   // sample rate (same as base format)
            16,               // sample size in bits (thx to Javazoom)
            baseFormat.getChannels(),     // # of Channels
            baseFormat.getChannels()*2,   // Frame Size
            baseFormat.getSampleRate(),   // Frame Rate
            false                 // Big Endian
        );
        AudioInputStream decodedInput = AudioSystem.getAudioInputStream(decodedFormat, rawInput);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...