LineUnavailableException for playing mp3 with java

前端 未结 2 961
孤街浪徒
孤街浪徒 2020-12-06 20:33

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

    AudioInputStream         


        
相关标签:
2条回答
  • 2020-12-06 21:16

    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();
      }
      

      }

    0 讨论(0)
  • 2020-12-06 21:34

    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);
    
    0 讨论(0)
提交回复
热议问题