java - 用Java录制即将到来的声音

标签 java audio record

在我的程序中,当用户说“会说话的汤姆猫”之类的东西时,我想录制声音,但是我只能找到一种总是开始录音的方式。
请帮我。对不起我的英语不好

最佳答案

这是使用Sound API在Java中捕获和录制声音的方法

import javax.sound.sampled.*;
import java.io.*;

/**
* A sample program is to demonstrate how to record sound in Java
* author: www.codejava.net
*/
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 60000;  // 1 minute

// path of the wav file
File wavFile = new File("E:/Test/RecordAudio.wav");

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

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

/**
 * 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 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("Start capturing...");

        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");
}

/**
 * Entry to run the program
 */
public static void main(String[] args) {
    final JavaSoundRecorder recorder = new JavaSoundRecorder();

    // creates a new thread that waits for a specified
    // of time before stopping
    Thread stopper = new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(RECORD_TIME);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            recorder.finish();
        }
    });

    stopper.start();

    // start recording
    recorder.start();
}
}

来源: http://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api

关于java - 用Java录制即将到来的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633617/

相关文章:

java - 如何按符号修剪/剪切java中的字符串?

android - Android:找不到声音文件

iphone - cocos2d静音问题

php - MySQL查询不符合查询条件时返回第0行

c# - 在WPF C#中使用NAudio录制音频

java - 如何在android中为不同的设备和方向创建不同的布局

java - 减去日期;日期格式

java - JDOM 中的元素列表

java - 同时通过 2 个不同的声卡播放 2 首音乐

postgresql - 从函数返回带有列定义的记录