0

当我使用 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 是即时的)。

4

0 回答 0