0
我有一个SoundManager
类和Sound
类设置,以便我可以从我的代码中的任何位置播放声音。我的问题是,在家时,声音片段播放良好(将其转换为.wav
的正确音频格式后)。然而,在学校,该程序崩溃,说音频格式不支持。有没有一种方法可以让音频在任何系统上运行,而不管Java版本如何?Java:声音传输问题
感谢
我有一个SoundManager
类和Sound
类设置,以便我可以从我的代码中的任何位置播放声音。我的问题是,在家时,声音片段播放良好(将其转换为.wav
的正确音频格式后)。然而,在学校,该程序崩溃,说音频格式不支持。有没有一种方法可以让音频在任何系统上运行,而不管Java版本如何?Java:声音传输问题
感谢
你可以看到,在Java听起来使用像一台机器上的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
的Wav应始终支持,但不一定是所有采样率等等。你得到的确切例外信息是什么? –
@ greg-449由于我目前不在学校,所以我不确定确切的消息,但是我的'.wav'的音频格式是:'PCM_SIGNED 44100.0Hz,16位,立体声,4字节/帧, endian',它说学校不支持 –
这看起来像普通的CD质量音频。系统可能不支持“小端”或类似的东西。您将不得不查看输出SourceDataLine支持的格式,以查看它是否与支持的格式匹配。 –