JAVA监听录音器软件设计Word格式.docx
《JAVA监听录音器软件设计Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA监听录音器软件设计Word格式.docx(17页珍藏版)》请在冰点文库上搜索。
3.3载入线程SplashWindow.java……………………………………………14
4设计总结…………………………………………………………………………15
1.前言
随着科学技术的不断发展,计算机已经成为我们工作学习和生活中不可缺少的工具。
文本编辑器是个人计算机最司空见惯的应用程序了,在学习了Java语言之后,我决定使用Java语言编写一个监听录音器软件,可以实现简单的监听录音功能,满足日常基本的工作学习和娱乐需要。
Java是由Sun公司开发的新一代纯面向对象的网络编程语言。
其目标是建立一种在任意种机器、任一种操作系统的网络环境中运行的软件,实行所谓的“程序写一次,到处运行”的目标。
正因为如此,Java已成为当今Internet上最流行、最受欢迎的一种程序开发语言。
Java开发小组把Java按特性分为基本版、移动版、企业版,每个版本有一个软件开发包。
Java基本版本叫Java2标准版(Java2StandardEdition,J2SE),它包含建立Java应用程序或者是Applet所需的应用程序编程接口(API)。
Java2移动版(TheJava2MobileEdition,J2ME)包含创建无线Java应用程序的API。
还有Java2企业版(TheJava2Enterprise,J2EE)是J2SE的增强版本,包含建立多层架构应用程序API。
Java语言是由C++语言发展起而来的,是一种彻底的面向对象的程序设计语言。
作为一种纯面向对象的程序设计语言,它非常适合大型软件的开发。
Java语言去掉了C++语言的一些容易引起错误的特性。
Java语言的特点有:
面向对象,跨平台,安全性,多线程和图形功能强。
关键字:
Java录音,录音类,监听录音器
2.概要设计
2.1课程设计的目的
1、设计一个带有载入页面和有录音,停止,播放功能的监听录音器。
这个小软件能方便我们的使用和操作,为我们以后的使用创造一个良好的平台。
2、把自己所学的理论知识运用到实际操作中去,以此来发现自己的不足,及时的改正。
在实际操作中学习以及运用还没有学过的知识。
最后还可以在调试中提升自己。
2.2开发环境
开发平台:
MicrosoftWindowsXPProfessionalServicePack2
开发工具:
JBuilder2007+JDK1.6.0_02
2.3监听录音器功能
功能1:
有个载入界面,显示正在加载监听录音器程序和一张图片
功能2:
按下“录音”按钮,程序注册录音事件
功能3:
当按下“录音”按钮,“停止”按钮就能控制,按下此按钮录音停止
功能4:
按“播放”按钮
,播放刚刚录制的声音
功能5:
按下“保存按钮”键,声音被保存到D盘
2.4界面设计
图2.3载入界面
图2.4程序界面
2.5系统结构图
3详细设计
系统有主程序类RecordPlay类和两个载入线程类SplashThread和SplashWindow类
3.1主程序RecordPlay.java:
packageRecordPlay;
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);
//开始录音
capture();
}
});
add(captureBtn);
//注册停止事件
stopBtn.addActionListener(newActionListener()
playBtn.setEnabled(true);
saveBtn.setEnabled(true);
//停止录音
stop();
add(stopBtn);
//注册播放事件
playBtn.addActionListener(newActionListener()
//播放录音
play();
add(playBtn);
//注册保存事件
saveBtn.addActionListener(newActionListener()
//保存录音
capture();
save();
add(saveBtn);
//注册窗体关闭事件
addWindowListener(newWindowAdapter()
publicvoidwindowClosing(WindowEvente)
System.exit(0);
//设置窗体属性
Containercon=getContentPane();
con.setBackground(Color.blue);
setLayout(newFlowLayout());
setTitle("
12教技2班陈家辉—录音器"
setSize(500,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)
//
(2)播放ByteArrayOutputStream中的数据
privatevoidplay()
//取得录音数据
byteaudioData[]=byteArrayOutputStream.toByteArray();
//转换成输入流
InputStreambyteArrayInputStream=newByteArrayInputStream(
audioData);
AudioFormataudioFormat=getAudioFormat();
audioInputStream=newAudioInputStream(byteArrayInputStream,
audioFormat,audioData.length/audioFormat.getFrameSize());
SourceDataLine.class,audioFormat);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
//创建独立线程进行播放
ThreadplayThread=newThread(newPlayThread());
playThread.start();
//(3)停止录音
publicvoidstop()
stopCapture=true;
//(4)保存文件
publicvoidsave()
//取得录音输入流
InputStreambyteArrayInputStream=newByteArrayInputStream(audioData);
//写入文件
Filefile=newFile("
D:
/test.wav"
AudioSystem.write(audioInputStream,AudioFileFormat.Type.WAVE,file);
//取得AudioFormat
privateAudioFormatgetAudioFormat()
floatsampleRate=16000.0F;
//8000,11025,16000,22050,44100
intsampleSizeInBits=16;
//8,16
intchannels=1;
//1,2
booleansigned=true;
//true,false
booleanbigEndian=false;
returnnewAudioFormat(sampleRate,sampleSizeInBits,channels,signed,
bigEndian);
publicclassPlayThreadextendsThread
bytetempBuffer[]=newbyte[10000];
publicvoidrun()
intcnt;
//读取数据到缓存数据
while((cnt=audioInputStream.read(tempBuffer,0,
tempBuffer.length))!
=-1)
if(cnt>
0)
//写入缓存数据
sourceDataLine.write(tempBuffer,0,cnt);
//Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
publicclassCaptureThreadextendsThread
//临时数组
byteArrayOutputStream=newByteArrayOutputStream();
totaldatasize=0;
stopCapture=false;
try{//循环执行,直到按下停止录音按钮
while(!
stopCapture)
//读取10000个数据
intcnt=targetDataLine.read(tempBuffer,0,
tempBuffer.length);
//保存该数据
byteArrayOutputStream.write(tempBuffer,0,cnt);
totaldatasize+=cnt;
byteArrayOutputStream.close();
e.printStackTrace();
publicstaticvoidmain(Stringargs[])
newRecordPlay();
3.2载入线程SplashThread.java:
publicclassSplashThreadextendsThread
SplashWindowfp;
publicSplashThread(SplashWindowfp)
this.fp=fp;
while(fp.progressBar.getValue()<
20)
fp.progressBar.setValue(fp.progressBar.getValue()+1);
try
Thread.sleep(200);
}catch(InterruptedExceptionex)
fp.dispose();
3.3载入线程SplashWindow.java:
importjava.awt.*;
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);
Threadth=newSplashThread(this);
th.start();
publicstaticvoidmain(String[]args)
newSplashWindow();
4.实验总结:
在网上寻找作品的程序,然后再写入软件中,这一过程遇到许多问题,经过XX和查教科书,总是有办法成功解决问题的。
不过这个实验做得有点遗憾,想加入一个倒时和一个声音时间记录,虽然经过同学和XX的帮忙,但是现在还没有成功,我以后一定要解决它。
学习java时觉得很有乐趣,我已经决