1

我将在 java 程序中将一些音频输出到具有多个通道(8 个通道)的声卡。为此,我想在声卡上的不同通道上发送不同的声音(以便可以在不同的扬声器上播放)发送的音频是不同的文件(可能是 mp3)。

有人对如何使用 java 执行此操作有任何建议吗?到目前为止,我尝试过的是 javax.sound.sampled 库。我确实设法在扬声器上发出了一些声音,但没有决定使用哪些频道。我尝试过使用 Port.Info,但似乎无法处理它的语法。

这是到目前为止的代码,这项工作:

// open up an audio stream
private static void init() {
    try {
        // 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian
        AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        //Port.Info port = new Port.Info((Port.class), Port.Info.SPEAKER, false);

        //Port.Info pi = new Port.Info((Port.class), "HEADPHONES", false);

        line = (SourceDataLine) AudioSystem.getLine(info);
        //line = (SourceDataLine) AudioSystem.getLine(port);
        line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);

        // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
        // it gets divided because we can't expect the buffered data to line up exactly with when
        // the sound card decides to push out its samples.
        buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.exit(1);
    }

    // no sound gets made before this call
    line.start();
}
4

0 回答 0