Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我正在使用一个线程来捕获第一次起作用的音频输入。但是,我得到了一个java.lang.IllegalThreadStateException,我发现这是因为未设置中断标志。我能得到的任何帮助都会很棒的:)下面是一个完全可编译的示例。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainFrame extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setBounds(100, 100, 618, 373);

        Sessioninprogress sip = new Sessioninprogress(this);
        sip.setVisible(true);
        setContentPane(sip);
        setLayout(null);
    }
}




import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.sound.sampled.*;

import java.io.*;
import java.awt.Color;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

import javax.swing.JLabel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class Sessioninprogress extends JPanel {

     // path of the wav file
     File wavFile = new File("C:/userconvo.wav");

     // format of audio file
     AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

     // the line from which audio data is captured
     TargetDataLine line;

     ExecutorService executorService = Executors.newFixedThreadPool(1);

 public Sessioninprogress(final MainFrame parent) {


 setBounds(100, 100, 618, 373);
 setBackground(new Color(255, 250, 250));
 setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
 setLayout(null);

     JLabel txtpnEmployeeLogin = new JLabel();
     txtpnEmployeeLogin.setForeground(Color.DARK_GRAY);
     txtpnEmployeeLogin.setBackground(Color.WHITE);
     txtpnEmployeeLogin.setFont(new Font("Tahoma", Font.PLAIN, 34));
     txtpnEmployeeLogin.setText("Session in progress");
     txtpnEmployeeLogin.setBounds(150, 123, 409, 52);
     add(txtpnEmployeeLogin);

   final JButton captB = new JButton();
   captB.setFont(new Font("Tahoma", Font.PLAIN, 14));
   captB.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
   captB.setBounds(225, 228, 153, 52);
   captB.setText("Start");
   captB.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           if (captB.getText().startsWith("Start")) {
               captB.setText("Stop");
               executorService.execute(new Runnable() {
                    public void run() {
                        beginCapture();                 }
                });
             } else {
               captB.setText("Start");
               finish();
             }

       }
   });
   add(captB);




 }

 // * Defines an audio format
 AudioFormat getAudioFormat() {
     float sampleRate = 16000;
     int sampleSizeInBits = 8;
     int channels = 2;
     boolean signed = true;
     boolean bigEndian = true;
     AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                                          channels, signed, bigEndian);
     return format;
 }
 // * Captures the sound and record into a WAV file
 void beginCapture() {
     try {
         AudioFormat format = getAudioFormat();
         DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

         // checks if system supports the data line
         if (!AudioSystem.isLineSupported(info)) {
             System.out.println("Line not supported");
             System.exit(0);
         }
         line = (TargetDataLine) AudioSystem.getLine(info);
         line.open(format);
         line.start();
         AudioInputStream ais = new AudioInputStream(line);

         System.out.println("Start recording...");

         // start recording
         AudioSystem.write(ais, fileType, wavFile);

     } catch (LineUnavailableException ex) {
         ex.printStackTrace();
     } catch (IOException ioe) {
         ioe.printStackTrace();
     }
 }

  //* Closes the target data line to finish capturing and recording
 void finish() {
     line.stop();
     line.close();
     System.out.println("Finished");
 }
}


更新:

这是工作示例。我通过切换到ExecutorService解决了该问题。

最佳答案

问题在于:

if(!t.isAlive())
    t.start();


一个线程只能启动一次。从JavaDoc


  一次启动一个线程永远是不合法的。特别是
  完成执行后,线程可能无法重新启动。


这就是为什么它第一次起作用,而不是在随后的尝试中起作用的原因。

关于java - 未设置中断标志,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21615307/

10-09 09:25