2

使用 java sound api 录制时,我得到了巨大的回声。但我希望录音没有任何回声。我怎样才能做到这一点 ?或者我的以下两个班级可能有什么问题?

记录器.java

package media;

import javax.sound.sampled.*;

public class Recorder extends Thread{
    public static final int DEFAULT_AUDIO_RECORD_FRAME_DURATION = 60; /// 60ms
    private TargetDataLine line;
    private boolean running;

    public Recorder(){
        super();
    }
    @Override
    public void run(){
        running = true;
        try {
            

            AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.exit(0);
            }

            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();   // start capturing

            AudioInputStream ais = new AudioInputStream(line);
            byte [] data = new byte[1960]; /////frame size 1960. 60 ms data
            int readLen;
            int sequenceNumber = 0;
            try {
                while (running) {
                    readLen = ais.read(data);
                    if(readLen <= 0) {
                        break;
                    }
                    processEncodingAndSendToServer(data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                line.stop();
            }catch (Exception e){}
            try{
                line.close();
            }catch (Exception e){}
        }
    }
    public void shutDown(){
        running = false;
    }
}

播放器.java

package media;


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class Player extends Thread{
    private boolean running;

    public Player(){
        super();
    }

    @Override
    public void run(){

        int readLen;
        running = true;
        SourceDataLine sline = null;
        int sequenceNumber = 0;
        try{

            AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
            byte [] data = new byte[1960];
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

            sline = (SourceDataLine) AudioSystem.getLine(info);

            sline.open(format);
            sline.start();   // start capturing

            while(running){
                readLen = getPacketForPlay(data, 0);  ////get a packet of length 1960 after deocoding which is received from server
                if(readLen <= 0) {
                    continue;
                }
                sline.write(data, 0 , readLen);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                sline.stop();
                sline.close();
            }catch (Exception e){}
        }
    }

    public void shutDown(){
        running = false;
        this.interrupt();
    }
}

以上两个类是在这个会议应用程序运行时独立运行的简单线程。即使在两个人的谈话中,我也得到了巨大的回响。而这个问题随着更多人的通话而变得更加严重。奇怪的是,即使使用耳机连接的电脑也会出现这个问题。我确定我的代码在这里犯了一些错误。迫切需要一些帮助。

4

0 回答 0