sound.sampled.Clip を使用して mp3 ファイルをテストすると、期待どおりに再生されます。ただし、OpenALでテストすると、音も例外もありません。
(私は JMF の mp3plugin.jar と OpenAL ライブラリを使用しています)
これは単純なコードで説明するのが難しい問題です。なぜなら、openal を使用するには多くのライブラリと少しのセットアップ コードが必要だからです。しかし、私は最善を尽くしました。
public static void main(String[] args) throws Exception {
init(); //initialises OpenAL's device, context and alCapabilities
Thread.sleep(1000);
System.out.println("Clip");
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("grumble.mp3"));
ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start(); //plays both wav and mp3
Thread.sleep(2000);
clip.close();
ais.close();
System.out.println("OpenAL");
ais = AudioSystem.getAudioInputStream(new File("grumble.mp3"));
ais = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais);
AudioFormat format = ais.getFormat();
byte[] byteArr = new byte[ais.available()];
ais.read(byteArr);
ByteBuffer buf = BufferUtils.createByteBuffer(byteArr.length);
buf.put(byteArr);
byteArr = null;
ais.close();
buf.flip();
int buffer = alGenBuffers();
alBufferData(buffer, getALFormat(format), buf, (int)format.getSampleRate() );
int source = alGenSources();
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source); //plays wav, but is silent when trying to play mp3
Thread.sleep(2000);
System.out.println("end");
alDeleteSources(source);
alcCloseDevice(device);
}
public static int getALFormat(AudioFormat format)
{
int alFormat = -1;
if (format.getChannels() == 1) {
System.out.println("mono ");
if (format.getSampleSizeInBits() == 8) {
alFormat = AL_FORMAT_MONO8;
} else if (format.getSampleSizeInBits() == 16) {
alFormat = AL_FORMAT_MONO16;
} else {
System.out.println("sample size: "+format.getSampleSizeInBits());
}
} else if (format.getChannels() == 2) {
System.out.println("stereo ");
if (format.getSampleSizeInBits() == 8) {
alFormat = AL_FORMAT_STEREO8;
} else if (format.getSampleSizeInBits() == 16) {
alFormat = AL_FORMAT_STEREO16;
} else {
System.out.println("sample size: "+format.getSampleSizeInBits());
}
}
return alFormat;
}
これがコンソールに表示されるすべてです:
Clip
OpenAL
stereo
end
なぜ OpenAL を使用する必要があるのか疑問に思われている方のために説明すると、OpenAL は、Clip の方がはるかに扱いやすいパン操作とピッチ操作の両方を提供します。私は渡した任意の wav ファイルをパンおよびピッチすることができましたが、Clip はどちらもできないことがよくあります (また、パン/ゲインを調整するときに遅延が発生しますが、OpenAL はインスタントです)。