我无法将TargetDataLine转换为线路

huangapple go评论68阅读模式
英文:

I'm unable to cast TargetDataLine to a line

问题

以下是您提供的代码的翻译:

public class Audio3 {

    public static void main(String[] args) throws LineUnavailableException, InterruptedException {
        
        // 音频格式设置
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
        
        // 音频数据线信息设置
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        final SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
        sourceLine.open();
        
        info = new DataLine.Info(SourceDataLine.class, format);
        final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
        targetLine.open();
        
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        
        // 源数据线程
        Thread sourceThread = new Thread() {
            
            @Override
            public void run() {
                sourceLine.start();
                
                while (true) {
                    // out.size() 也正常工作
                    sourceLine.write(out.toByteArray(), 0, out.toByteArray().length);
                }
            }
        };
        
        // 目标数据线程
        Thread targetThread = new Thread() {
            
            @Override
            public void run() {
                targetLine.start();
                byte[] data = new byte[targetLine.getBufferSize() / 5];
                int readBytes;
                
                while (true) {
                    readBytes = targetLine.read(data, 0, data.length);
                    out.write(data, 0, readBytes);
                }
            }
        };
    }
}

请注意,我已按照您的要求,只返回了翻译好的代码部分。如果您有任何其他问题或需要进一步的帮助,请随时提问。

英文:

The program below i have works by recording from microphone for 5 sec and then it would automatically replay it. However i'm getting an error from casting TargetDataLine to Audio.getLine(). Any help is very appriciated!

public class Audio3 {
public static void main(String[] args) throws LineUnavailableException, InterruptedException {
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
final SourceDataLine sourceLine = (SourceDataLine)AudioSystem.getLine(info);
sourceLine.open();
info = new DataLine.Info(SourceDataLine.class, format);
final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
targetLine.open();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Thread sourceThread = new Thread() {
@Override
public void run() {
sourceLine.start();
while(true) {
//out.size() also works fine
sourceLine.write(out.toByteArray(), 0, out.toByteArray().length);
}
}
};
Thread targetThread = new Thread() {
@Override
public void run() {
targetLine.start();
byte[] data = new byte[targetLine.getBufferSize() / 5];
int readBytes;
while(true) {
readBytes = targetLine.read(data, 0, data.length);
out.write(data, 0, readBytes);
}
}
};
}
}

答案1

得分: 1

SourceDataLine.class更改为TargetDataLine.class

    info = new DataLine.Info(TargetDataLine.class, format); // 更改过
    final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
    targetLine.open();
英文:

Change SourceDataLine.class to TargetDataLine.class:

    info = new DataLine.Info(TargetDataLine.class, format); // changed
    final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
    targetLine.open();

huangapple
  • 本文由 发表于 2020年9月3日 03:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63712640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定