jToggleButton的while循环错误:JAVA
Posted
技术标签:
【中文标题】jToggleButton的while循环错误:JAVA【英文标题】:while loop error with jToggleButton : JAVA 【发布时间】:2017-02-15 12:38:15 【问题描述】:我在 netbeans 中使用 while 循环编写了一个用于音频捕获的函数。但是 while 循环代码中的最后一个大括号显示编译错误,我不知道为什么(代码中通过注释提到了该行)。无论如何我运行该项目,它说“编译错误”并运行。运行应用程序后,我单击jToggleButton
启动编码功能(音频捕获),它成功运行。在我再次单击jToggleButton
以停止该功能(音频捕获)后,它也会停止。但是在第二次单击之后,netbeans 输出终端也显示了一些错误。所以,我试图找出问题所在,但仍然无法确定代码出了什么问题。我希望,我能从这里得到帮助。
错误日志:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:153)
at javaapplication1.NewJFrame1$2.run(NewJFrame1.java:83)
at java.lang.Thread.run(Thread.java:745)
BUILD SUCCESSFUL (total time: 10 seconds)
代码:
package javaapplication1;
import java.io.ByteArrayOutputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Audiosystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class NewJFrame1 extends javax.swing.JFrame
public NewJFrame1()
initComponents();
@SuppressWarnings("unchecked")
private void initComponents()
jPanel1 = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jToggleButton1.setText("jToggleButton1");
jToggleButton1.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent evt)
jToggleButton1ActionPerformed(evt);
);
jPanel1.add(jToggleButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 70, -1, -1));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt)
Thread t = new Thread(new Runnable()
@Override
public void run()
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
TargetDataLine microphone;
SourceDataLine speakers;
try
microphone = AudioSystem.getTargetDataLine(format);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
int CHUNK_SIZE = 1024;
byte[] data = new byte[microphone.getBufferSize() / 5];
microphone.start();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
speakers.open(format);
speakers.start();
while(jToggleButton1.isSelected())
numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
speakers.write(data, 0, numBytesRead);
speakers.drain();
speakers.close();
microphone.close();
catch (LineUnavailableException e)
e.printStackTrace();
// This Carly Bracket shows red underline. but I don't know whyv?!
t.start();
public static void main(String args[])
try
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName()))
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (InstantiationException ex)
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (IllegalAccessException ex)
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (javax.swing.UnsupportedLookAndFeelException ex)
java.util.logging.Logger.getLogger(NewJFrame1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new NewJFrame1().setVisible(true);
);
// Variables declaration
private javax.swing.JPanel jPanel1;
private javax.swing.JToggleButton jToggleButton1;
【问题讨论】:
出现错误提示您的声音库无法处理声音文件的格式。 您需要添加);在你的下划线括号后面关闭你的新线程 谢谢@KilledByCheese。我得到了它。现在该错误已修复。但是netbeans输出终端显示错误(有问题)仍然存在。你能帮助我吗...?我试过了,还是不知道。 您好@SteveSmith,那我该如何解决...?你能帮帮我吗? @user7560586 不要删除原始问题中得到回答的部分,如果解决了就接受回答。因为其他人可能想在将来引用它。 【参考方案1】:// 这个 Carly 括号显示红色下划线。但我不知道为什么v?!
阅读编译器错误,它说用 );
关闭此语句因为你还没有关闭下面的语句:
Thread t = new Thread ( new Runnable()
【讨论】:
非常感谢@Rohit Gulati,我明白了。现在该错误已修复。但是netbeans输出终端显示错误(有问题提到)仍然存在。你能帮助我吗...?我试过了,还是不知道。 @user7560586 你能试试这个solution,因为这里提到的问题完全一样。 @user7560586 如上面的链接中所述,您在代码中打开了两行代码。当您尝试打开第二个时,第一个的存在会阻止它。麦克风。打开(格式);和 speaker.open(format); @user7560586我也是新手,但我尝试运行您的代码,当我注释掉代码“microphone.open(format)”时,它现在正在抛出异常。试图只打开一条线。执行此操作后检查您的功能是否按预期工作。以上是关于jToggleButton的while循环错误:JAVA的主要内容,如果未能解决你的问题,请参考以下文章