允许Java应用程序在录制音频时继续运行

时间:2015-01-19 21:33:18

标签: java swing audio recording

我正在尝试嵌入一些我发现的代码(原始here)来记录和保存音频输入到Java swing应用程序中。我的问题是我希望音频录制与应用程序正在执行的其他操作同时运行,但实际上,应用程序会暂停,直到录音结束。我怎样才能防止这种情况发生?

这是我的代码的简化版本,它会产生问题。显示“现在录制”的行直到录音结束后才会出现。

import javax.sound.sampled.*;

import java.io.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;

import javafx.embed.swing.JFXPanel;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example extends JPanel {

    protected static final long serialVersionUID = 1L;

    // whether we've started recording
    private boolean startedRecording;

    // record duration, in milliseconds
    static final long RECORD_TIME = 5000;  // 5 seconds

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

    // path of the wav file
    File wavFile;

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

    public Example( String output_fn ) {
        wavFile = new File(output_fn);
        startedRecording = false;
        setPreferredSize(new Dimension(100,100));
        createPanel();
    }

    public static JFXPanel createPanel() {
        return new JFXPanel();
    }

    /**
     * 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
     */
    public void start() {
        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();   // start capturing

            System.out.println("In utils.Recorder: Start capturing...");

            AudioInputStream ais = new AudioInputStream(line);

            System.out.println("In utils.Recorder: Start recording...");

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

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

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2   = (Graphics2D)g;
        // start recording
        if ( !startedRecording ) {
            startedRecording = true;
            Thread stopper = new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(RECORD_TIME);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    Example.this.finish();
                }
            });
            stopper.start();
            this.start();
        }
        // display message
        g2.drawString("Now recording", 50, 50);
    }

    public static void main(String[] args) {
        final Example eg = new Example("TestRecordAudio.wav");
        JFrame f    = new JFrame();
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.add(eg, BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
        f.repaint();
    }
}

2 个答案:

答案 0 :(得分:4)

绘画方法是绘画!永远不要在paint方法中执行任何其他操作,这根本不合适,而且非常非常糟糕。

创建JButton(或JToggleButton)并使用此功能开始/停止录制。

使用line.stop()line.close()停止录制。

Swing是一个单线程环境,在事件调度线程的上下文中执行的任何长时间运行或阻塞代码都会阻止它处理事件队列,使其看起来像程序已挂起,因为它具有。< / p>

您可以使用SwingWorker,但由于您在录制时没有尝试更新用户界面,因此使用Thread

会更容易

看看

了解更多详情

更新了示例

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestRecord {

    public static void main(String[] args) {
        new TestRecord();
    }

    public TestRecord() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        private JToggleButton recordButton;

        protected static final AudioFileFormat.Type FILE_TYPE = AudioFileFormat.Type.WAVE;
        private TargetDataLine line;

        public TestPane() {
            setLayout(new GridBagLayout());
            recordButton = new JToggleButton("Record");
            recordButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (recordButton.isSelected()) {
                        startRecording();
                        recordButton.setText("Stop");
                    } else {
                        stopRecording();
                        recordButton.setText("Record");
                    }
                }
            });
            add(recordButton);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void stopRecording() {

            if (line != null) {

                line.stop();
                line.close();
                line = null;

            }

        }

        protected void startRecording() {
            if (line == null) {
                Thread t = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        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();   // start capturing

                            System.out.println("In utils.Recorder: Start capturing...");

                            AudioInputStream ais = new AudioInputStream(line);

                            System.out.println("In utils.Recorder: Start recording...");

                            // start recording
                            System.out.println("Is recoding");
                            AudioSystem.write(ais, FILE_TYPE, new File("Test.wav"));

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

                        System.out.println("Recording is done");
                    }
                });
                t.start();
            }
        }

        protected 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;
        }
    }

}

(nb:我无法测试它,因为我没有可用于支持录制的线路......显然)

答案 1 :(得分:1)

您在AWT Event Dispatch Thread上呼叫start,从而阻止其他任何事情发生。您需要在另一个线程上调用您的音频呼叫代码。

你永远不应该在Swing线程上运行长时间运行的任务,除非你想要面对死亡的灰色屏幕