java手机录音代码 安卓录音代码( 六 )


import java.io.File;
import java.io.InputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
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.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class RecordPlay {
boolean stopCapture = false; // 控制录音标志
AudioFormat audioFormat; // 录音格式
// 读取数据:从TargetDataLine写入ByteArrayOutputStream录音
ByteArrayOutputStream byteArrayOutputStream;
int totaldatasize = 0;
TargetDataLine targetDataLine;
// 播放数据:从AudioInputStream写入SourceDataLine播放
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
private Button captureBtn;
private Button stopBtn;
private Button playBtn;
private Button saveBtn;
private Label myLabel;
private Shell shell;
private Display display;
public RecordPlay() {
super();
display = new Display();
shell = new Shell(display);
shell.setSize(350, 150);
shell.setText("录音机程序");
//
myLabel = new Label(shell, SWT.NONE);
myLabel.setBounds(38, 21, 100, 18);
myLabel.setText("录音机");
// 创建按钮
captureBtn = new Button(shell, SWT.NONE);
captureBtn.setBounds(30, 61, 60, 18);
captureBtn.setText("录音");
captureBtn.setEnabled(true);
stopBtn = new Button(shell, SWT.NONE);
stopBtn.setBounds(100, 61, 60, 18);
stopBtn.setText("停止");
stopBtn.setEnabled(false);
playBtn = new Button(shell, SWT.NONE);
playBtn.setBounds(170, 61, 60, 18);
playBtn.setText("播放");
playBtn.setEnabled(false);
saveBtn = new Button(shell, SWT.NONE);
saveBtn.setBounds(240, 61, 60, 18);
saveBtn.setText("保存");
saveBtn.setEnabled(false);
// 注册录音事件
captureBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
// 开始录音
capture();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册停止事件
stopBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
// 停止录音
stop();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册播放事件
playBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 播放录音
play();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
// 注册保存事件
saveBtn.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
// 保存录音
save();
}
public void widgetDefaultSelected(SelectionEvent event) {
// text.setText("No worries!");
}
});
}
public void start() {
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void main(String[] args) {
RecordPlay label = new RecordPlay();
label.start();
}
// (1)录音事件,保存到ByteArrayOutputStream中

推荐阅读