【问题标题】:Java: sound transportation issuesJava:健全的交通问题
【发布时间】:2015-12-15 20:30:22
【问题描述】:

我设置了一个SoundManager 类和一个Sound 类,这样我就可以在我的代码中的任何位置播放声音。我的问题是,在家时,声音片段可以正常播放(在将它们转换为.wav 的正确音频形式之后)。但是,在学校,程序崩溃了,说不支持音频格式。有没有办法让音频在任何系统上运行,无论 java 版本如何?

谢谢

【问题讨论】:

  • 应始终支持 Wav,但不一定支持所有采样率等。您收到的确切异常消息是什么?
  • @greg-449 不确定确切的信息,因为我目前不在学校,但我的.wav 的音频格式是:PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian,它说不支持在学校
  • 这看起来像普通的 CD 质量音频。可能系统不支持“小端”或类似的东西。您必须查看输出 SourceDataLine 支持的格式,看看它是否与支持的格式匹配。
  • @greg-449 我将如何查看这些输出?
  • 对于Mixer 调用mixer.getSourceLineInfo() 以获取Line.Info 的数组。如果Line.InfoDataLine.Info 的一个实例,则可以调用getFormats() 来获取支持的音频格式数组。

标签: java audio format system wav


【解决方案1】:

您可以使用以下方式在机器上查看 Java Sounds API 支持的格式:

public static void displayMixerInfo()
{
  Mixer.Info [] mixersInfo = AudioSystem.getMixerInfo();

  for (Mixer.Info mixerInfo : mixersInfo)
   {
     System.out.println("Mixer: " + mixerInfo.getName());

     Mixer mixer = AudioSystem.getMixer(mixerInfo);

     Line.Info [] sourceLineInfo = mixer.getSourceLineInfo();
     for (Line.Info info : sourceLineInfo)
      {
        showLineInfo(info);
      }

     Line.Info [] targetLineInfo = mixer.getTargetLineInfo();
     for (Line.Info info : targetLineInfo)
      {
        showLineInfo(info);
      }
   }
}


private static void showLineInfo(Line.Info lineInfo)
{
  System.out.println("  " + lineInfo.toString());

  if (lineInfo instanceof DataLine.Info)
   {
     DataLine.Info dataLineInfo = (DataLine.Info)lineInfo;

     AudioFormat [] formats = dataLineInfo.getFormats();
     for (AudioFormat format : formats)
      {
        System.out.println("    " + format.toString());
      }
   }
}

在我的 Mac 上显示:

Mixer: Default Audio Device
  interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
  interface Clip supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
  interface TargetDataLine supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Built-in Microphone
  interface TargetDataLine supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Built-in Output
  interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
  interface Clip supporting 14 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Port Built-in Microphone
  MICROPHONE source port
Mixer: Port Built-in Output
  HEADPHONE target port

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多