我设置了SoundManager
类和Sound
类,以便我可以在代码中的任何位置播放声音。我的问题是,当在家时,声音片段播放正常(将它们转换为.wav
的正确音频形式)。但是,在学校,程序崩溃,说音频格式不受支持。有没有一种方法可以让音频在任何系统上运行,无论Java版本如何?
由于
答案 0 :(得分:2)
您可以使用以下内容查看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