我无法将TargetDataLine转换为线路

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

I'm unable to cast TargetDataLine to a line

问题

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

  1. public class Audio3 {
  2. public static void main(String[] args) throws LineUnavailableException, InterruptedException {
  3. // 音频格式设置
  4. AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
  5. // 音频数据线信息设置
  6. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  7. final SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
  8. sourceLine.open();
  9. info = new DataLine.Info(SourceDataLine.class, format);
  10. final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
  11. targetLine.open();
  12. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  13. // 源数据线程
  14. Thread sourceThread = new Thread() {
  15. @Override
  16. public void run() {
  17. sourceLine.start();
  18. while (true) {
  19. // out.size() 也正常工作
  20. sourceLine.write(out.toByteArray(), 0, out.toByteArray().length);
  21. }
  22. }
  23. };
  24. // 目标数据线程
  25. Thread targetThread = new Thread() {
  26. @Override
  27. public void run() {
  28. targetLine.start();
  29. byte[] data = new byte[targetLine.getBufferSize() / 5];
  30. int readBytes;
  31. while (true) {
  32. readBytes = targetLine.read(data, 0, data.length);
  33. out.write(data, 0, readBytes);
  34. }
  35. }
  36. };
  37. }
  38. }

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

英文:

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!

  1. public class Audio3 {
  2. public static void main(String[] args) throws LineUnavailableException, InterruptedException {
  3. AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
  4. DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  5. final SourceDataLine sourceLine = (SourceDataLine)AudioSystem.getLine(info);
  6. sourceLine.open();
  7. info = new DataLine.Info(SourceDataLine.class, format);
  8. final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
  9. targetLine.open();
  10. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  11. Thread sourceThread = new Thread() {
  12. @Override
  13. public void run() {
  14. sourceLine.start();
  15. while(true) {
  16. //out.size() also works fine
  17. sourceLine.write(out.toByteArray(), 0, out.toByteArray().length);
  18. }
  19. }
  20. };
  21. Thread targetThread = new Thread() {
  22. @Override
  23. public void run() {
  24. targetLine.start();
  25. byte[] data = new byte[targetLine.getBufferSize() / 5];
  26. int readBytes;
  27. while(true) {
  28. readBytes = targetLine.read(data, 0, data.length);
  29. out.write(data, 0, readBytes);
  30. }
  31. }
  32. };
  33. }
  34. }

答案1

得分: 1

SourceDataLine.class更改为TargetDataLine.class

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

Change SourceDataLine.class to TargetDataLine.class:

  1. info = new DataLine.Info(TargetDataLine.class, format); // changed
  2. final TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(info);
  3. 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:

确定