英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论