java - 在多个输出中播放音频

标签 java audio

我需要在不同的音频输出中播放音乐。
例如,我有两首音乐:music1 和 music2,
他们必须在不同的扬声器中以不同的线程播放。
假设我有不止一个音频设备可以
播放声音:

我找到了这个方法(here - 它是 BasicPlayer):

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

通过一点调试,我发现混音器始终为空。这是为什么?
调音台不应该是通过目标线路输出声音的设备吗?
这个程序总是在我电脑上设置的默认设备中播放,我该怎么做才能改变呢?

最佳答案

实际上,我刚刚开始为我自己的一个项目使用 Java Sound API,但据我了解,Mixer 只是一个接口(interface),而不是一个对象。这可以解释你的问题的一部分。

关于java - 在多个输出中播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478426/

相关文章:

jquery - .next() .prev() 遍历多个 <article> 元素

audio - 在 Android (1.5) 中禁用麦克风的噪音消除?

visual-studio-2010 - 使用C++/CLI时不会产生声音

java - Android 意外错误

java - 即使脚本在数据库客户端 (MySQL) 中运行正常,Flyway 迁移也会失败

java - 使用 java 进行英语到西类牙语翻译

html - InfoWindow内部的音频播放器(Google map )

audio - μ法律算法实现

java - 将 null 值保存为多个键,其中 null 具有单一含义

java - 选择一些代码,用一些方法名称覆盖它,并在完成参数时自动输入先前选择的代码[在 Intellij 中]