user7560586
user7560586

Reputation: 19

while loop error with jToggleButton : JAVA

I coded a function for Audio capturing with while loop in netbeans. but last curly bracket in the while loop code shows compilation error and I don't know why (the line is mentioned in code by comment). Anyhow I run the project, and it says "compile with errors" and ran. After running the app, I click the jToggleButton to start the coded function(Audio Capturing) and it worked successfully. after I click the jToggleButton again to stop the function(Audio Capturing), it gets stopped also. but after this second click, the netbeans output terminal shows some errors as well. So, I tried to find what is the issue, but still couldn't identified what's going wrong with code. I hope, I can get help from here.

Error logs :

 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) 

Code:

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;

}

Upvotes: 0

Views: 109

Answers (1)

Rohit Gulati
Rohit Gulati

Reputation: 542

// This Carly Bracket shows red underline. but I don't know whyv?!

Read the compiler error, it says to close this statement with );

Because you have not closed the below statement:

 Thread t = new Thread ( new Runnable()  

Upvotes: 1

Related Questions