软件程序设计语音录放机.docx

上传人:b****2 文档编号:17824345 上传时间:2023-08-04 格式:DOCX 页数:17 大小:209.41KB
下载 相关 举报
软件程序设计语音录放机.docx_第1页
第1页 / 共17页
软件程序设计语音录放机.docx_第2页
第2页 / 共17页
软件程序设计语音录放机.docx_第3页
第3页 / 共17页
软件程序设计语音录放机.docx_第4页
第4页 / 共17页
软件程序设计语音录放机.docx_第5页
第5页 / 共17页
软件程序设计语音录放机.docx_第6页
第6页 / 共17页
软件程序设计语音录放机.docx_第7页
第7页 / 共17页
软件程序设计语音录放机.docx_第8页
第8页 / 共17页
软件程序设计语音录放机.docx_第9页
第9页 / 共17页
软件程序设计语音录放机.docx_第10页
第10页 / 共17页
软件程序设计语音录放机.docx_第11页
第11页 / 共17页
软件程序设计语音录放机.docx_第12页
第12页 / 共17页
软件程序设计语音录放机.docx_第13页
第13页 / 共17页
软件程序设计语音录放机.docx_第14页
第14页 / 共17页
软件程序设计语音录放机.docx_第15页
第15页 / 共17页
软件程序设计语音录放机.docx_第16页
第16页 / 共17页
软件程序设计语音录放机.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

软件程序设计语音录放机.docx

《软件程序设计语音录放机.docx》由会员分享,可在线阅读,更多相关《软件程序设计语音录放机.docx(17页珍藏版)》请在冰点文库上搜索。

软件程序设计语音录放机.docx

软件程序设计语音录放机

软件程序设计

语音录放机

 

院系:

电子信息工程

姓名:

学号:

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();

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2