简体   繁体   English

使用java播放mp3的LineUnavailableException

[英]LineUnavailableException for playing mp3 with java

My goal is to play an mp3 file from Java. 我的目标是从Java播放mp3文件。 With every approach that I took, it always fails with a LineUnavailableException . 对于我采用的每种方法,它总是因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. 使用Sun的mp3插件。
  • Use Jlayer 3rd party library 使用Jlayer第三方库
  • Use Tritonus 3rd party library 使用Tritonus第三方库
  • Re-encode the mp3 with Sony Sound Forge, Adobe Sound Booth, all no luck 使用Sony Sound Forge,Adobe Sound Booth重新编码mp3,一切都没有运气
  • Re-encode the mp3 with different encode rates and sampling rates 使用不同的编码率和采样率重新编码mp3
  • Try to use JMF 尝试使用JMF
  • Use random mp3 from the Internet that plays fine in other applications 使用来自互联网的随机mp3,在其他应用程序中可以正常播放
  • 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)

Apparently, the mp3 has to be read into one stream. 显然,mp3必须被读入一个流。 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);

OK - Let's start by ruling out your MP3 files and your code. 好的 - 让我们首先排除你的MP3文件和你的代码。

  1. Pick an MP3 file that you have and play it with any MP3 player. 选择您拥有的MP3文件并使用任何MP3播放器播放。
  2. Download http://www.javazoom.net/javalayer/sources/jlayer1.0.1.zip 下载http://www.javazoom.net/javalayer/sources/jlayer1.0.1.zip
  3. Extract jl1.0.1.jar from zip file and put in your classpath 从zip文件中提取jl1.0.1.jar并放入类路径
  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. 编译并运行以确保步骤1中的mp3文件是文件的参数。 (In my case I had this "C:\\\\Users\\\\romain\\\\Music\\\\Al DiMeola\\\\Elegant Gypsy\\\\01 Flight over Rio Al DiMeola.mp3") (在我的情况下,我有这个“C:\\\\用户\\\\ romain \\\\音乐\\\\ Al DiMeola \\\\优雅的吉普赛\\ \\ 01飞越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(); } 

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM