软件程序设计语音录放机.docx
- 文档编号:17824345
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:17
- 大小:209.41KB
软件程序设计语音录放机.docx
《软件程序设计语音录放机.docx》由会员分享,可在线阅读,更多相关《软件程序设计语音录放机.docx(17页珍藏版)》请在冰点文库上搜索。
软件程序设计语音录放机
软件程序设计
语音录放机
院系:
电子信息工程
姓名:
学号:
1121103711211044
一.引言
系统:
录音机播放器。
目的:
实现音频的录制,播放和保存。
背景:
NerBeans7.0.1
参考资料:
JAVA教科书,XX。
二.系统概述
本系统是一个录音机录制,播放和保存系统。
它可以实现录音的录制,播放和保存。
由一个主程序类RecordPlay类和两个载入线程类SplashThread和SplashWindow类。
三.系统功能结构设计
四.主要界面
载入界面:
程序界面:
五.使用说明
运行SplashWindow
打开之后点击“录音”,对着麦克风说话,想要停止的时候点“停止”。
停止之后可以选择“播放”,也可以直接“保存”。
附件(程序清单)
主程序RecordPlay.java:
packageRecordPlay;
/**
*
*@authorAdministrator
*/
importjava.awt.Color;
importjava.awt.Container;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.File;
importjava.io.InputStream;
importjavax.swing.*;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.sound.sampled.AudioFileFormat;
importjavax.sound.sampled.AudioFormat;
importjavax.sound.sampled.AudioInputStream;
importjavax.sound.sampled.AudioSystem;
importjavax.sound.sampled.DataLine;
importjavax.sound.sampled.SourceDataLine;
importjavax.sound.sampled.TargetDataLine;
publicclassRecordPlayextendsJFrame
{
booleanstopCapture=false;//控制录音标志
AudioFormataudioFormat;//录音格式
//读取数据:
从TargetDataLine写入ByteArrayOutputStream录音
ByteArrayOutputStreambyteArrayOutputStream;
inttotaldatasize=0;
TargetDataLinetargetDataLine;
//播放数据:
从AudioInputStream写入SourceDataLine播放
AudioInputStreamaudioInputStream;
SourceDataLinesourceDataLine;
publicRecordPlay()
{
//创建按钮
finalJButtoncaptureBtn=newJButton("录音");
finalJButtonstopBtn=newJButton("停止");
finalJButtonplayBtn=newJButton("播放");
finalJButtonsaveBtn=newJButton("保存");
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//注册录音事件
captureBtn.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
captureBtn.setEnabled(false);
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
saveBtn.setEnabled(false);
//开始录音
capture();
}
});
add(captureBtn);
//注册停止事件
stopBtn.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
captureBtn.setEnabled(true);
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
//停止录音
stop();
}
});
add(stopBtn);
//注册播放事件
playBtn.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
//播放录音
play();
}
});
add(playBtn);
//注册保存事件
saveBtn.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
//保存录音
save();
}
});
add(saveBtn);
//注册窗体关闭事件
addWindowListener(newWindowAdapter()
{
@Override
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
//设置窗体属性
Containercon=getContentPane();
con.setBackground(Color.gray);
setLayout(newFlowLayout());
setTitle("LH录音机");
setSize(350,200);
setVisible(true);
}
//
(1)录音事件,保存到ByteArrayOutputStream中
privatevoidcapture()
{
try{
//打开录音
audioFormat=getAudioFormat();
DataLine.InfodataLineInfo=newDataLine.Info(
TargetDataLine.class,audioFormat);
targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
//创建独立线程进行录音
ThreadcaptureThread=newThread(newCaptureThread());
captureThread.start();
}catch(Exceptione)
{
System.exit(0);
}
}
//
(2)播放ByteArrayOutputStream中的数据
privatevoidplay()
{
try{
//取得录音数据
byteaudioData[]=byteArrayOutputStream.toByteArray();
//转换成输入流
InputStreambyteArrayInputStream=newByteArrayInputStream(
audioData);
AudioFormataudioFormat=getAudioFormat();
audioInputStream=newAudioInputStream(byteArrayInputStream,
audioFormat,audioData.length/audioFormat.getFrameSize());
DataLine.InfodataLineInfo=newDataLine.Info(
SourceDataLine.class,audioFormat);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
//创建独立线程进行播放
ThreadplayThread=newThread(newPlayThread());
playThread.start();
}catch(Exceptione)
{
System.exit(0);
}
}
//(3)停止录音
publicvoidstop()
{
stopCapture=true;
}
//(4)保存文件
publicvoidsave()
{
//取得录音输入流
AudioFormataudioFormat=getAudioFormat();
byteaudioData[]=byteArrayOutputStream.toByteArray();
InputStreambyteArrayInputStream=newByteArrayInputStream(audioData);
audioInputStream=newAudioInputStream(byteArrayInputStream,
audioFormat,audioData.length/audioFormat.getFrameSize());
//写入文件
try{
Filefile=newFile("D:
/test.wav");
AudioSystem.write(audioInputStream,AudioFileFormat.Type.WAVE,file);
}catch(Exceptione)
{
}
}
//取得AudioFormat
privateAudioFormatgetAudioFormat()
{
floatsampleRate=16000.0F;
//8000,11025,16000,22050,44100
intsampleSizeInBits=16;
//8,16
intchannels=1;
//1,2
booleansigned=true;
//true,false
booleanbigEndian=false;
//true,false
returnnewAudioFormat(sampleRate,sampleSizeInBits,channels,signed,
bigEndian);
}
publicclassPlayThreadextendsThread
{
bytetempBuffer[]=newbyte[10000];
publicvoidrun()
{
try{
intcnt;
//读取数据到缓存数据
while((cnt=audioInputStream.read(tempBuffer,0,
tempBuffer.length))!
=-1)
{
if(cnt>0)
{
//写入缓存数据
sourceDataLine.write(tempBuffer,0,cnt);
}
}
//Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
}catch(Exceptione)
{
System.exit(0);
}
}
}
publicclassCaptureThreadextendsThread
{
//临时数组
bytetempBuffer[]=newbyte[10000];
@Override
publicvoidrun()
{
byteArrayOutputStream=newByteArrayOutputStream();
totaldatasize=0;
stopCapture=false;
try{//循环执行,直到按下停止录音按钮
while(!
stopCapture)
{
//读取10000个数据
intcnt=targetDataLine.read(tempBuffer,0,
tempBuffer.length);
if(cnt>0)
{
//保存该数据
byteArrayOutputStream.write(tempBuffer,0,cnt);
totaldatasize+=cnt;
}
}
byteArrayOutputStream.close();
}catch(Exceptione)
{
e.printStackTrace();
System.exit(0);
}
}
}
publicstaticvoidmain(Stringargs[])
{
newRecordPlay();
}
}
载入线程SplashThread.java:
packageRecordPlay;
/**
*
*@authorAdministrator
*/
publicclassSplashThreadextendsThread
{
SplashWindowfp;
publicSplashThread(SplashWindowfp)
{
this.fp=fp;
}
@Override
publicvoidrun()
{
while(fp.progressBar.getValue()<20)
{
fp.progressBar.setValue(fp.progressBar.getValue()+1);
try
{
Thread.sleep(200);
}catch(InterruptedExceptionex)
{
}
}
fp.dispose();
newRecordPlay();
}
}
载入线程SplashWindow.java:
packageRecordPlay;
/**
*
*@authorAdministrator
*/
importjava.awt.*;
importjavax.swing.*;
/**
*
*@authorAdministrator
*/
publicclassSplashWindowextendsJWindow
{
JLabelback=newJLabel(newImageIcon("1.jpg"));
JProgressBarprogressBar=newJProgressBar(1,20);
publicSplashWindow()
{
Containercon=this.getContentPane();
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
progressBar.setStringPainted(true);
progressBar.setString("正在加载程序...");
con.add(back,"Center");
con.add(progressBar,"South");
setSize(400,300);
toFront();
Dimensionsize=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((size.width-getWidth())/2,
(size.height-getHeight())/2);
setVisible(true);
Threadth=newSplashThread(this);
th.start();
}
publicstaticvoidmain(String[]args)
{
newSplashWindow();
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 软件 程序设计 语音 录放机
![提示](https://static.bingdoc.com/images/bang_tan.gif)