Java: 良好的交通问题
Java: sound transportation issues
我设置了 SoundManager
class 和 Sound
class 以便我可以在我的代码中的任何地方播放声音。我的问题是,在家里时,声音片段可以正常播放(在将它们转换为 .wav
的正确音频格式之后)。然而,在学校,程序崩溃,说不支持音频格式。有没有办法让我在任何系统上获得 运行 的音频,而不考虑 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
我设置了 SoundManager
class 和 Sound
class 以便我可以在我的代码中的任何地方播放声音。我的问题是,在家里时,声音片段可以正常播放(在将它们转换为 .wav
的正确音频格式之后)。然而,在学校,程序崩溃,说不支持音频格式。有没有办法让我在任何系统上获得 运行 的音频,而不考虑 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