Change Mixer to output sound to in java

限于喜欢 提交于 2019-12-01 11:20:14

问题


I am trying to play a wav/mp3 to my virtual audio cable, I have been searching for hours but can't seem to find how to achieve this. I have been able to play sound in both formats but I can't get it to output to 'Line-1' rather than 'Speakers'

Any helpful links or example code would be greatly appreciated.


回答1:


To get an array of all Mixers on the current platform, you may use AudioSystem#getMixerInfo:

static void printAllMixerNames() {
    for(Mixer.Info info : AudioSystem.getMixerInfo()) {
        System.out.println(info.getName());
    }
}

If your virtual cable is available, it will be in the array. For example, on my Mac the following is printed:

Java Sound Audio Engine
Built-in Input
Soundflower (2ch)
Soundflower (64ch)
Pro Tools Aggregate I/O

(Soundflower is a virtual device.)

To get some specific Mixer you unfortunately need to do String evaluation. So you need to discover its name, vendor, whatever, beforehand or give the user an option to pick one from a list.

static Mixer getMixerByName(String toFind) {
    for(Mixer.Info info : AudioSystem.getMixerInfo()) {
        if(toFind.equals(info.getName())) {
            return AudioSystem.getMixer(info);
        }
    }
    return null;
}

Once you have a particular Mixer you can obtain a Line or AudioInputStream from it. You can obtain a Clip from it through AudioSystem#getClip(Mixer.Info).

I am trying to play a wav/mp3...

javax.sound.sampled does not support mp3. Alternatives can be found here.



来源:https://stackoverflow.com/questions/27026042/change-mixer-to-output-sound-to-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!