如何使java程序充当音频输出设备?

时间:2018-05-07 01:39:35

标签: java audio iostream

我正在尝试创建一个充当音频设备的程序,以便我可以修改通过它的所有程序的音量,然后将音频发送到另一个真实播放设备。我对java课程进行了一些研究。并找到了看起来很有前途的AudioSystem课程,但我不知道如何获得所有单独的节目音频线路。

我正在制作的程序几乎是Windows音量调音台的简化复制品,我打算用它来做更多,但这正是我的开始。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Control;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class SystemTests {

    public static Mixer headPhones;
    public static final int inputInfo = 10;
    public static Mixer.Info[] mi;
    public static Line.Info[] ti;
    public static SourceDataLine line;
    public static AudioFormat format;
    public static final int recordTime = 10000;
    static AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    static File wavFile = new File("c://temp//testFile1.wav");

    public static void main(String[] args) {
        final SystemTests recorder = new SystemTests();

        //create a thread for stopping the process
        Thread stop = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(recordTime);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
//              Scanner in = new Scanner(System.in);
//              System.out.println("Press Enter to stop Recording.");
//              in.nextLine();
//              recorder.finish();
                System.exit(0);
            }
        });

        stop.start();
        recorder.start();
    }

    public void start(){
        mi = AudioSystem.getMixerInfo();
        System.out.println(mi[inputInfo]);

        //show all mixer info
//      for(int i = 0; i < mi.length; i++) {
//          System.out.println(mi[i] + " _ " + i);
//      }

        //get mixer and line info
        headPhones = AudioSystem.getMixer(mi[inputInfo]);
        ti = headPhones.getSourceLineInfo();

        //show all line info
//      for(int i = 0; i < ti.length; i++) {
//          System.out.println(ti[i] + " _ " + i);
//      }

        //get the line 
//      try {
//          line = (SourceDataLine) headPhones.getLine(ti[0]);
//      } catch (LineUnavailableException e) {
//          e.printStackTrace();
//      }

        Control[] controls = AudioSystem.getMixer(mi[inputInfo]).getControls();
        for(int i = 0; i < controls.length; i++) {
            System.out.println(controls[i]);
        }
    }

    public void finish() {
        line.stop();
        line.close();
        System.out.println("Finished");
    }

}

0 个答案:

没有答案