java - 为什么我会得到 java.lang.IllegalArgumentException : No line matching interface SourceDataLine supporting format MPEG1L3 44100. 0 ....

标签 java mp3 javasound

我已经在我的 netbeans 项目所需的运行时库中设置了 mp3plugin.jar 。但是当我尝试播放 mp3 时,我仍然遇到上述异常 文件。这是什么原因:

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class tester_1  {
private static final int EXTERNAL_BUFFER_SIZE = 128000;



public static void main(String[] args)
{
    /*
      We check that there is exactely one command-line
      argument.
      If not, we display the usage message and exit.
    */


    /*
      Now, that we're shure there is an argument, we
      take it as the filename of the soundfile
      we want to play.
    */
    String  strFilename = "mp3tester.mp3";
    File    soundFile = new File("mp3tester.mp3");

    /*
      We have to read in the sound file.
    */
    AudioInputStream    audioInputStream = null;
    try
    {
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    }
    catch (Exception e)
    {
        /*
          In case of an exception, we dump the exception
          including the stack trace to the console output.
          Then, we exit the program.
        */
        e.printStackTrace();
        System.exit(1);
    }

    /*
      From the AudioInputStream, i.e. from the sound file,
      we fetch information about the format of the
      audio data.
      These information include the sampling frequency,
      the number of
      channels and the size of the samples.
      These information
      are needed to ask Java Sound for a suitable output line
      for this audio file.
    */
    AudioFormat audioFormat = audioInputStream.getFormat();

    /*
      Asking for a line is a rather tricky thing.
      We have to construct an Info object that specifies
      the desired properties for the line.
      First, we have to say which kind of line we want. The
      possibilities are: SourceDataLine (for playback), Clip
      (for repeated playback)   and TargetDataLine (for
      recording).
      Here, we want to do normal playback, so we ask for
      a SourceDataLine.
      Then, we have to pass an AudioFormat object, so that
      the Line knows which format the data passed to it
      will have.
      Furthermore, we can give Java Sound a hint about how
      big the internal buffer for the line should be. This
      isn't used here, signaling that we
      don't care about the exact size. Java Sound will use
      some default value for the buffer size.
    */
    SourceDataLine  line = null;
    DataLine.Info   info = new DataLine.Info(SourceDataLine.class,
                                             audioFormat);
    try
    {
        line = (SourceDataLine) AudioSystem.getLine(info);

        /*
          The line is there, but it is not yet ready to
          receive audio data. We have to open the line.
        */
        line.open(audioFormat);
    }
    catch (LineUnavailableException e)
    {
        e.printStackTrace();
        System.exit(1);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }

    /*
      Still not enough. The line now can receive data,
      but will not pass them on to the audio output device
      (which means to your sound card). This has to be
      activated.
    */
    line.start();

    /*
      Ok, finally the line is prepared. Now comes the real
      job: we have to write data to the line. We do this
      in a loop. First, we read data from the
      AudioInputStream to a buffer. Then, we write from
      this buffer to the Line. This is done until the end
      of the file is reached, which is detected by a
      return value of -1 from the read method of the
      AudioInputStream.
    */
    int nBytesRead = 0;
    byte[]  abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1)
    {
        try
        {
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if (nBytesRead >= 0)
        {
            int nBytesWritten = line.write(abData, 0, nBytesRead);
        }
    }

    /*
      Wait until all data are played.
      This is only necessary because of the bug noted below.
      (If we do not wait, we would interrupt the playback by
      prematurely closing the line and exiting the VM.)

      Thanks to Margie Fitch for bringing me on the right
      path to this solution.
    */
    line.drain();

    /*
      All data are played. We can close the shop.
    */
    line.close();

    /*
      There is a bug in the jdk1.3/1.4.
      It prevents correct termination of the VM.
      So we have to exit ourselves.
    */
    System.exit(0);
}


private static void printUsageAndExit()
{
    out("tester_1: usage:");
    out("\tjava tester_1 <soundfile>");
    System.exit(1);
}


private static void out(String strMessage)
{
    System.out.println(strMessage);
}

}

当我运行这个程序时,出现以下异常:

java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, unknown frame rate,  is supported.
at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:476)
at mp3tester_mp3plugin.tester_1.main(tester_1.java:178)
 Java Result: 1

我收到此错误的原因是什么?

最佳答案

import java.io.File;

import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;

public class AudioTest {
public static void main(String[] args) {
    Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
    Format input2 = new AudioFormat(AudioFormat.MPEG);
    Format output = new AudioFormat(AudioFormat.LINEAR);
    PlugInManager.addPlugIn(
        "com.sun.media.codec.audio.mp3.JavaDecoder",
        new Format[]{input1, input2},
        new Format[]{output},
        PlugInManager.CODEC
    );
    try{
        Player player = Manager.createPlayer(new MediaLocator(new File("mp3tester.mp3").toURI().toURL()));
        player.start();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}
}

morgenstille.at 提供.

干杯, Vim

PS Oracle“隐藏”了 JMF 库 here .

编辑:实际回答您的问题:javax.sound 在播放“某些”mp3 格式时存在问题,而 javax.media 和 mp3 插件的问题较少(不是?)。 检查 JavaZoom 等 API 或其他 API 以满足您的 mp3 需求可能是值得的。

关于java - 为什么我会得到 java.lang.IllegalArgumentException : No line matching interface SourceDataLine supporting format MPEG1L3 44100. 0 ....,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6884974/

相关文章:

java - android是否会杀死单例以释放内存?

JavaFX 播放 mp3

google-chrome - 在 Chrome 中流式传输 <audio> 时的播放延迟,但在 Firefox 中则不然

java - 音频 - 在 Java 中字节到 Int 并返回

java - AudioClip 麻烦

java - JSON 响应字符串中包含自动添加的反斜杠

java - 如何将列表转换为唯一列表?

Java 接口(interface)和类型

java - 用于 Java 应用程序的 CDDB API

java - 剪辑不播放任何声音