本人网上收集的两个java录音程序,文章出处不记得了.
程序1:
import javax.sound.sampled.*;
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import javax.sound.sampled.AudioInputStream;
import javax.swing.JButton;
import java.io.File;
import java.util.Vector;
import java.awt.BorderLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.BoxLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioSystem;
import java.io.IOException;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
* @see java录音程序
*
*
*/
public class RecordTest extends JPanel implements ActionListener {
final int bufSize = 16384;
Capture capture = new Capture();
AudioInputStream audioInputStream;
JButton captB;
String fileName = "untitled";
String errStr;
double duration, seconds;
File file;
Vector lines = new Vector();
public RecordTest() {
setLayout(new BorderLayout());
EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);
SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel p1 = new JPanel();
p1.setBorder(sbb);
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBorder(new EmptyBorder(10, 0, 5, 0));
captB = addButton("Record", buttonsPanel, true);
p1.add(buttonsPanel);
add(p1);
}
public void open() {
}
public void close() {
if (capture.thread != null)
captB.doClick(0);
}
private JButton addButton(String name, JPanel p, boolean state) {
JButton b = new JButton(name);
b.addActionListener(this);
b.setEnabled(state);
p.add(b);
return b;
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj.equals(captB)) {
if (captB.getText().startsWith("Record")) {
file = null;
capture.start();
fileName = "untitled";
captB.setText("Stop");
} else {
lines.removeAllElements();
capture.stop();
captB.setText("Record");
}
}
}
public void createAudioInputStream(File file, boolean updateComponents) {
if (file != null && file.isFile()) {
try {
this.file = file;
errStr = null;
audioInputStream = AudioSystem.getAudioInputStream(file);
fileName = file.getName();
long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / audioInputStream
.getFormat().getFrameRate());
duration = milliseconds / 1000.0;
if (updateComponents) {
}
} catch (Exception ex) {
reportStatus(ex.toString());
}
} else {
reportStatus("Audio file required.");
}
}
public void saveToFile(String name, AudioFileFormat.Type fileType) {
if (audioInputStream == null) {
reportStatus("No loaded audio to save");
return;
} else if (file != null) {
createAudioInputStream(file, false);
}
// reset to the beginnning of the captured data
try {
audioInputStream.reset();
} catch (Exception e) {
reportStatus("Unable to reset stream " + e);
return;
}
File file = new File(fileName = name);
try {
if (AudioSystem.write(audioInputStream, fileType, file) == -1) {
throw new IOException("Problems writing to file");
}
} catch (Exception ex) {
reportStatus(ex.toString());
}
}
private void reportStatus(String msg) {
if ((errStr = msg) != null) {
System.out.println(errStr);
}
}
class Capture implements Runnable {
TargetDataLine line;
Thread thread;
public void start() {
errStr = null;
thread = new Thread(this);
thread.setName("Capture");
thread.start();
}
public void stop() {
thread = null;
}
private void shutDown(String message) {
if ((errStr = message) != null && thread != null) {
thread = null;
captB.setText("Record");
System.err.println(errStr);
}
}
public void run() {
duration = 0;
audioInputStream = null;
// get an AudioInputStream of the desired format for playback
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
// define the required attributes for our line,
// and make sure a compatible line is supported.
// float rate = 44100f;
// int sampleSize = 16;
// String signedString = "signed";
// boolean bigEndian = true;
// int channels = 2;
float rate = 8000f;
int sampleSize = 8;
String signedString = "signed";
boolean bigEndian = true;
int channels = 1;
AudioFormat format = new AudioFormat(encoding, rate, sampleSize,
channels, (sampleSize / 8) * channels, rate, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
shutDown("Line matching " + info + " not supported.");
return;
}
// get an AudioInputStream of the desired format for playback
try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
} catch (SecurityException ex) {
shutDown(ex.toString());
return;
} catch (Exception ex) {
shutDown(ex.toString());
return;
}
// play back the captured audio data
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
line.start();
while (thread != null) {
if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
break;
}
out.write(data, 0, numBytesRead);
}
// we reached the end of the stream. stop and close the line.
line.stop();
line.close();
line = null;
// stop and close the output stream
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
// load bytes into the audio input stream for playback
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(bais, format,
audioBytes.length / frameSizeInBytes);
long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format
.getFrameRate());
duration = milliseconds / 1000.0;
try {
audioInputStream.reset();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
saveToFile("record.wav", AudioFileFormat.Type.WAVE);
}
}
public static void main(String[] args) {
RecordTest test = new RecordTest();
test.open();
JFrame f = new JFrame("Capture");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.getContentPane().add("Center", test);
f.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = 500;
int h = 340;
f.setLocation(screenSize.width / 2 - w / 2, screenSize.height / 2 - h
/ 2);
f.setSize(w, h);
f.show();
}
}
程序2:
import java.io.ByteArrayOutputStream;
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;
public class CopyOfJDKAudioRecorder extends Thread {
// 產生TargetDataLine類別的變數m_targetdataline
static TargetDataLine m_targetdataline;
// 透過TargetDataLine介面(繼承自DataLine)與音效卡溝通 target目標
// 產生AudioFileFormat.Type類別的變數m_targetType Format格式
static AudioFileFormat.Type m_targetType;
// 產生AudioInputStream類別的變數m_audioInputStream stream流
static AudioInputStream m_audioInputStream;
static File m_outputFile;// 產生File類別的變數 m_outputFile
static ByteArrayOutputStream bos = new ByteArrayOutputStream();
static byte[] buf;
static boolean m_bRecording;// 後面需用到布林函數 True,False
public CopyOfJDKAudioRecorder(TargetDataLine line,
AudioFileFormat.Type targetType, File file) {
m_targetdataline = line;
m_audioInputStream = new AudioInputStream(line);
m_targetType = targetType;
m_outputFile = file;
}
public static void AudioRecorder() {
String Filename = "d:/JDKAudioRecord.wav ";
File outputFile = new File(Filename);
// 我們一開始先在主程式裡指定聲音檔的檔名為
// JDKAudioRecorder.wav
// String Filename = "JDKAudioRecord.wav ";
// 接著指定存檔的資料夾,預設存在相同的資料夾
// File outputFile = new File(Filename);
AudioFormat audioFormat = null;
// audioFormat = new
// AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100.0F, 16, 2, 4,
// 44100.0F, false);
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,
8, 1, 1, 44100F, false);
// 再來設定和取得音效檔的屬性
// audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
// 44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
audioFormat);
TargetDataLine targetDataLine = null;
// 然後透過TargetDataLine介面(繼承自DataLine)與音效卡溝通
// DataLine.Info info = new DataLine.Info(TargetDataLine.class,
// audioFormat);
// 接著做例外處理,當聲音裝置出錯或其他因素導致錄音功能無法被執行時,程式將被終止
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);// try{ }可能發生例外的敘述
} catch (LineUnavailableException e)// catch{ }處理方法
{
System.out.println("無法錄音,錄音失敗 ");
e.printStackTrace();
System.exit(-1);
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.AU;
CopyOfJDKAudioRecorder recorder = null;
recorder = new CopyOfJDKAudioRecorder(targetDataLine, targetType,
outputFile);
recorder.start();
}
public void start() {
m_targetdataline.start();
super.start();
System.out.println("recording...");
}
public static void stopRecording() {
m_targetdataline.stop();
m_targetdataline.close();
m_bRecording = false;
buf = bos.toByteArray();
System.out.println("stopped.");
}
public void run() {
try {
// AudioSystem.write(m_audioInputStream, m_targetType,
// m_outputFile);
AudioSystem.write(m_audioInputStream, m_targetType, bos);
System.out.println("after write() ");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
CopyOfJDKAudioRecorder.AudioRecorder();
}
}
分享到:
相关推荐
Java录音程序是使用Java语言开发的一种能够从计算机的麦克风捕获音频流并将其保存为WAV格式文件的应用。在本文中,我们将深入探讨如何利用Java实现这一功能,以及涉及的相关技术点。 首先,Java提供了Java Sound ...
Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小程序(3)个Java录音小...
java录音小程序,也是网载的,本人测试录音成功,分享给有需要的人。 提示:如果你使用无法录音,请试试在其它机器上如何,可能是你机器的问题,本人就吃过这样的亏。
本设计源码提供了一个基于Java的录音与音频播放demo。项目包含39个文件,主要使用Java编程语言。文件类型包括10个XML配置文件、10个WEBP图片文件、9个Java源代码文件、3个Gradle文件、2个GIT忽略文件、2个Properties...
java的录音程序,录音文件格式为wav文件,16位,8K采样,单通道。
总的来说,实现一个Java录音机需要对Java Sound API有深入的理解,包括音频格式、数据流的处理以及资源管理。通过上述步骤,你可以创建一个基本的录音程序,根据需求还可以扩展功能,如添加播放、剪辑、音效处理等。
在这个Java录音机程序中,主要是通过创建一个处理器来捕捉声音数据,并将捕捉到的数据通过一个DataSink 对象发送到指定的目的地,如网络上,具体到本例中将是一个指定的本地文件。 运行环境:Java/Eclipse
这个“Android录音程序源码.zip”文件提供了一个完整的Android应用项目,旨在帮助开发者学习如何在自己的应用中实现录音功能。源码通常包含了项目结构、布局文件、Java代码以及必要的资源配置,为深入理解Android...
根据提供的文件信息,我们可以分析出该Java程序是一个简单的录音机应用程序。下面将对该程序的关键知识点进行详细解析。 ### 关键知识点 #### 1. Java IO流处理 在本程序中,作者利用了Java中的IO流来进行数据的...
Java制造的录音程序,一个基于JMF 的简易录音机。用它可以把通过麦克风等输入的声音数据保存到磁盘文件中。保存的媒体文件格式可以是au、wav等。主要是通过创建一个处理器来捕捉声音数据,并将捕捉到的数据通过一个...
在IT领域,录音功能是许多应用程序的核心组成部分,特别是在语音识别、说话人识别等场景..."录音机"这个压缩包文件,很可能是包含了一个简单的C++录音程序示例,通过学习其源代码,可以加深对以上知识点的理解和应用。
针对"android2.2 录音程序完整源码"这个主题,我们将深入探讨Android 2.2(Froyo)版本下的录音API,以及如何实现一个简单的录音应用。这段源码为初学者提供了实践和理解Android录音机制的良好机会。 Android录音...
windows java app录音,转wav格式,使用欧拉蜜语音识别的JAVA SDK包测试语音识别
录音程序在IT领域中扮演着重要的角色,尤其在语音处理和通信系统中。"带有端点检测的语音录音程序"是一种高效且智能的工具,它能够精确地捕捉和记录人类语音,而忽略不必要的静音部分。这个程序的核心是端点检测...
本篇文章将深入解析Android录音程序的源码,并探讨其中的关键知识点。 首先,我们来了解一下Android录音的基础知识。在Android系统中,录音功能主要通过`MediaRecorder`类来实现。`MediaRecorder`是Android提供的一...
从所提供的文件内容来看,这些文字包含了关于《Java语言程序设计》(Brief Version,第八版)一书的相关信息,该书的作者是Y. Daniel Liang,他是来自Armstrong Atlantic State University的一位教授。这本书由...
在录音程序中,我们主要关注音频部分。以下是一些关键步骤: 1. **初始化**:首先,你需要实例化`MediaRecorder`对象,并设置其输出格式。通常,我们会选择常见的音频格式如AMR_NB(Adaptive Multi-Rate Narrowband...
基于Java语言的微信小程序录音机后台管理系统是专门为小程序录音功能设计的后台服务,它具备强大的数据处理和管理能力,能够有效地支持录音数据的存储、检索、编辑和管理。该系统不仅提供了稳定可靠的后台支持,也...
- 要实现录音功能,可以使用Java的`javax.sound.sampled`包。它提供了对音频输入和输出的低级控制,包括麦克风的音频输入。 - 音频数据需要实时捕获并存储,与视频帧同步。 4. **暂停与恢复** - 录制过程中,...