Clip peut jouer mp3 mais OpenAL donne juste le silence (Java)


Lorsque je teste un fichier mp3 en utilisant le son.échantillonner.Clip, il joue juste comme prévu. Quand je le teste avec OpenAL cependant, je n'obtiens aucun son, et aucune exception.

(j'utilise le mp3plugin de JMF.jar ainsi que les bibliothèques OpenAL)

C'est un problème difficile à démontrer avec du code simple, car travailler avec openal nécessite un certain nombre de bibliothèques et un peu de code de configuration, mais j'ai fait de mon mieux:

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;
}

Et c'est tout ce que je reçois console:

Clip
OpenAL
stereo 
end

Dans le cas où vous vous demandez pourquoi j'ai besoin d'utiliser OpenAL, OpenAL fournit à la fois un panoramique et une manipulation de hauteur avec laquelle le clip est beaucoup plus délicat. J'ai pu faire un panoramique et lancer n'importe quel fichier wav que je lui donne, alors que Clip ne le fera souvent pas non plus (et a une latence lors du réglage du panoramique/gain, alors que OpenAL est instantané).

Author: Perry Monschau, 2018-08-12