Java 在多个输出中播放音频
我需要以不同的音频输出播放音乐。 例如,我有两种音乐:音乐1和音乐2, 他们必须在不同的扬声器中以不同的线程播放。 假设我有多个音频设备可以 要播放声音: 我找到了这个方法(-它是BasicLayer):Java 在多个输出中播放音频,java,audio,Java,Audio,我需要以不同的音频输出播放音乐。 例如,我有两种音乐:音乐1和音乐2, 他们必须在不同的扬声器中以不同的线程播放。 假设我有多个音频设备可以 要播放声音: 我找到了这个方法(-它是BasicLayer): protectedvoid createLine()引发LineUnavailableException { log.info(“创建行”); 如果(m_线==null) { AudioFormat sourceFormat=m_audioInputStream.getFormat(); lo
protectedvoid createLine()引发LineUnavailableException
{
log.info(“创建行”);
如果(m_线==null)
{
AudioFormat sourceFormat=m_audioInputStream.getFormat();
log.info(“创建行:源格式:”+sourceFormat.toString());
int nSampleSizeInBits=sourceFormat.getSampleSizeInBits();
如果(nSampleSizeInBits我实际上刚刚开始为我自己的一个项目使用Java声音API,但据我所知,混音器只是一个接口,而不是一个对象。这可以解释问题的一部分
protected void createLine() throws LineUnavailableException
{
log.info("Create Line");
if (m_line == null)
{
AudioFormat sourceFormat = m_audioInputStream.getFormat();
log.info("Create Line : Source format : " + sourceFormat.toString());
int nSampleSizeInBits = sourceFormat.getSampleSizeInBits();
if (nSampleSizeInBits <= 0) nSampleSizeInBits = 16;
if ((sourceFormat.getEncoding() == AudioFormat.Encoding.ULAW) || (sourceFormat.getEncoding() == AudioFormat.Encoding.ALAW)) nSampleSizeInBits = 16;
if (nSampleSizeInBits != 8) nSampleSizeInBits = 16;
AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), nSampleSizeInBits, sourceFormat.getChannels(), sourceFormat.getChannels() * (nSampleSizeInBits / 8), sourceFormat.getSampleRate(), false);
log.info("Create Line : Target format: " + targetFormat);
// Keep a reference on encoded stream to progress notification.
m_encodedaudioInputStream = m_audioInputStream;
try
{
// Get total length in bytes of the encoded stream.
encodedLength = m_encodedaudioInputStream.available();
}
catch (IOException e)
{
log.error("Cannot get m_encodedaudioInputStream.available()", e);
}
// Create decoded stream.
m_audioInputStream = AudioSystem.getAudioInputStream(targetFormat, m_audioInputStream);
AudioFormat audioFormat = m_audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
Mixer mixer = getMixer(m_mixerName);
if (mixer != null)
{
log.info("Mixer : "+mixer.getMixerInfo().toString());
m_line = (SourceDataLine) mixer.getLine(info);
}
else
{
m_line = (SourceDataLine) AudioSystem.getLine(info);
m_mixerName = null;
}
log.info("Line : " + m_line.toString());
log.debug("Line Info : " + m_line.getLineInfo().toString());
log.debug("Line AudioFormat: " + m_line.getFormat().toString());
}
}