为什么将我只在Java改变了音频输入后听到的噪音? [英] Why do I get only hear noise after changing the audio input in java?

查看:500
本文介绍了为什么将我只在Java改变了音频输入后听到的噪音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我初始化的AudioInputStream 是这样的:

dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat()) ;
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(getAudioFormat());
targetDataLine.start();

这工作正常,我可以听到话筒的音频输入。

That works fine and I can hear the Audio Input of the Microphone.

如果我尝试将音频输入切换到另​​一台设备,我只听到噪音。我想现在就解决这个问题了一个星期,我真的没有线索了,为什么我不能听到其他音频输入设备......我会任何帮助非常自豪!

If I try to change the Audio Input to another device I only hear noise. I tried to solve this problem for a week now and I really don't have a clue anymore why I can't hear the other audio input device... I would be very proud about any help!

Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();

for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
    Mixer currentMixer = AudioSystem.getMixer(mixerInfo[cnt]);

    if (mixerInfo[cnt].getName() == combo1.getSelectedItem().toString()) {
        System.out.println("Gewählter Input gefunden");

        if (targetDataLine.isRunning()) {
            targetDataLine.stop();
        }

        targetDataLine.flush();

        if (targetDataLine.isOpen()) {
            targetDataLine.close();
        }

        dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
        try {
            targetDataLine = (TargetDataLine) currentMixer.getLine(dataLineInfo);
            targetDataLine.open(getAudioFormat());
            targetDataLine.start();

        } catch(LineUnavailableException e1) {
            e1.printStackTrace();
        }
    }
}

有关完整code,去 github上

For complete code, go to github

推荐答案

我相信这个问题是在的actionPerformed()方法:

I am certain the problem is in the actionPerformed() method:

dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
try {
    targetDataLine = (TargetDataLine) currentMixer.getLine(dataLineInfo) ;
    targetDataLine.open(getAudioFormat());
    targetDataLine.start();
    // TODO: You must read what's in the buffer here

} catch (LineUnavailableException e1) {
    e1.printStackTrace();
}

当第一次创建 Microphone2 对象,这部分被调用:

When the Microphone2 object is first created, this part is invoked:

public void run(){

    while(continueLoop){

        n = targetDataLine.read(tempBuffer, 0, tempBuffer.length);

        updateMeter();

        try {
            Object_Output_Stream.writeObject(tempBuffer);
            Object_Output_Stream.reset();

        } catch (IOException ex) {
            ex.printStackTrace();
            continueLoop = false;
        }
    }
}

如你所见,缓冲区读取和输出流充满什么在缓冲区中。这不会发生在的actionPerformed()被调用。

这篇关于为什么将我只在Java改变了音频输入后听到的噪音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆