鐐瑰嚮涓婃柟钃濊壊瀛椾綋锛岄€夋嫨鏍囨槦鍏紬鍙?/span>
浼樿川鏂囩珷锛岀涓€鏃堕棿閫佽揪
鍓嶅嚑娆″崥瀹腑涔熸槸鐢╦ava瀹炵幇褰曢煶鏈虹殑鍚勭鍔熻兘锛岃繖娆℃槸姣斿厛鍓嶆湁浜嗕竴鐐逛紭鍖栵紝灏嗗綍闊崇殑鍔熻兘鍗曠嫭鎻愬彇鍑烘潵锛岃繖涓▼搴忕殑瀹炵幇鐢ㄥ埌浜嗙紪鐮佸櫒LAME.exe杩欎釜鐨勪綔鐢ㄦ槸灏嗗綍闊冲緱鍒扮殑wav鏂囦欢缂栫爜鎴怣P3鏂囦欢锛岃繖鏍风殑缁撴灉鏄閲忓ぇ澶у噺灏戯紝鍥犱负LAME缂栫爜鍣ㄧ粡娴嬭瘯鍦ㄧ紪鐮佽繃绋嬩腑璺緞涓甫鏈夌┖鏍奸棶棰樻湭鑳借В鍐筹紝鎵€浠ュ彧鑳芥壘浜嗕竴涓鐨勬柟娉曪紝涓轰簡灏嗗叾鏀惧埌鎸囧畾鐨勬枃浠跺す涓?杩欎釜鏂囦欢澶硅矾寰勬槸甯︽湁绌烘牸鐨?锛屾墍浠ュ彧鑳藉厛瀛樻斁鍒癉鐩橈紝鐒跺悗鍒╃敤鏂囦欢鎿嶄綔灏嗗叾澶嶅埗鍒版寚瀹氱殑璺緞涓嬶紝鐒跺悗鍐嶅垹闄わ紝绋嬪簭涓繕鐢ㄥ埌浜嗗浘鐗囷紝淇敼浜唈ava鐨勫浘鏍囷紝鎵€浠ュ湪杩愯鏃跺彲灏嗗叾娉ㄩ噴鎺夛紝鍐欑殑寰堢儌锛岀浉浜掑涔犲搱锛?/span>
/*
* 瀹炵幇褰曢煶鏈虹殑鍔熻兘
* 1.鍦ㄥ師鏉ョ殑鍩虹涓婄晫闈㈡洿鍔犵簿绠€锛屽瓧浣撳彉澶т娇鐢ㄦ埛鏇村姞鏂逛究
* 2.鍦ㄥ仠姝㈠綍闊冲悗渚跨洿鎺ヤ繚瀛樹负.mp3鏂囦欢
* 3.瀵瑰仠姝㈠綍闊宠繘琛屾敼杩涳紝灏嗗綍闊虫寜閽璁℃垚绫讳技寰俊鐨勫舰寮忥紝鎸変笅鏃惰繘琛屽綍闊筹紝鏉惧紑鍗冲仠姝㈠綍闊冲苟淇濆瓨鍗冲彲
* 4.鍦ㄥ綍闊崇殑鏃跺€欏湪闈㈡澘涓婃樉绀洪煶棰戞尝锛岃〃绀烘鍦ㄥ綍闊?br> */
package com.liuyun.MyRecord6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import java.lang.*;
public class MyRecord6 extends JFrame implements MouseListener{
//瀹氫箟褰曢煶鏍煎紡
AudioFormat af = null;
//瀹氫箟鐩爣鏁版嵁琛?鍙互浠庝腑璇诲彇闊抽鏁版嵁,璇?TargetDataLine 鎺ュ彛鎻愪緵浠庣洰鏍囨暟鎹鐨勭紦鍐插尯璇诲彇鎵€鎹曡幏鏁版嵁鐨勬柟娉曘€?br> TargetDataLine td = null;
//瀹氫箟婧愭暟鎹,婧愭暟鎹鏄彲浠ュ啓鍏ユ暟鎹殑鏁版嵁琛屻€傚畠鍏呭綋鍏舵贩棰戝櫒鐨勬簮銆傚簲鐢ㄧ▼搴忓皢闊抽瀛楄妭鍐欏叆婧愭暟鎹锛岃繖鏍峰彲澶勭悊瀛楄妭缂撳啿骞跺皢瀹冧滑浼犻€掔粰娣烽鍣ㄣ€?br> SourceDataLine sd = null;
//瀹氫箟瀛楄妭鏁扮粍杈撳叆杈撳嚭娴?br> ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
//瀹氫箟闊抽杈撳叆娴?br> AudioInputStream ais = null;
//瀹氫箟鍋滄褰曢煶鐨勬爣蹇楋紝鏉ユ帶鍒跺綍闊崇嚎绋嬬殑杩愯
Boolean stopflag = false;
//璁板綍寮€濮嬪綍闊崇殑鏃堕棿
long startPlay;
//璁剧疆涓€涓挱鏀剧殑鏍囧織
Boolean playflag;
//姣忔淇濆瓨鐨勬渶鍚庣殑鏂囦欢鍚?br> File tarFile = null;
//瀹氫箟闊抽娉㈠舰姣忔鏄剧ず鐨勫瓧鑺傛暟
int intBytes = 0;
//瀹氫箟姣忔褰曢煶鐨勬椂鍊欐瘡娆℃彁鍙栧瓧鑺傛潵鐢婚煶棰戞尝
byte audioDataBuffer[] = null;
//瀹氫箟鎵€闇€瑕佺殑缁勪欢
JPanel jp1,jp2,jp3;
JLabel jl1=null;
JButton captureBtn;
//璁剧疆鐢绘尝褰㈢嚎绋嬬殑缁堟鐨勬爣蹇?br> boolean flag = true;
//瀹氫箟鎾斁褰曢煶鏃剁殑涓€涓鏁板€?br> int cnt;
//瀹氫箟鎾斁褰曢煶鏃朵竴涓紦鍐叉暟缁?br> byte btsPlay[] = null;
int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
double weightx, weighty;
Insets inset;
GridBagConstraints c;
public static void main(String[] args) {
//鍒涢€犱竴涓疄渚?br> MyRecord6 mr = new MyRecord6();
}
//鏋勯€犲嚱鏁?br> public MyRecord6()
{
//缁勪欢鍒濆鍖?br> jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
//瀹氫箟jp1鐨勫瓧浣?br> Font jpFont = new Font("鍗庢枃鏂伴瓘",Font.BOLD,40);
jl1 = new JLabel("璇风暀涓嬫偍鎯宠鐨勮瘽");
jl1.setFont(jpFont);
jl1.setForeground(Color.red);
jp1.add(jl1);
//瀹氫箟鎸夐挳涓婇潰鐨勫瓧浣?br> Font btFont = new Font("鍗庢枃鏂伴瓘",Font.BOLD,40);
captureBtn = new JButton("鎸変綇 璇磋瘽");
//setForeground鍙互璁剧疆鎸夐挳涓婇潰瀛椾綋鐨勯鑹?br> captureBtn.setForeground(Color.RED);
captureBtn.setFont(btFont);
//瀵瑰紑濮嬪綍闊虫寜閽繘琛岄紶鏍囩洃鍚?br> captureBtn.addMouseListener(this);
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.add(jp3,BorderLayout.SOUTH);
GridBagLayout gridbag = null;
jp3.setLayout(gridbag = new GridBagLayout());
gridx=1;
gridy=2;
gridwidth=1;
gridheight=1;
weightx=1;
weighty=1;
anchor=GridBagConstraints.CENTER;
fill=GridBagConstraints.HORIZONTAL;
inset=new Insets(1,1,1,1);
ipadx=0;
ipady=30;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, inset, ipadx, ipady);
gridbag.setConstraints(captureBtn, c);
jp3.add(captureBtn);
//璁剧疆绐楀彛鐨勫睘鎬?br> this.setSize(800,500);
this.setTitle("褰曢煶鏈?);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//璁剧疆绐楀彛灞呬腑
this.setLocationRelativeTo(null);
//灏嗙獥鍙g殑杈规鍘绘帀
this.setUndecorated(true);
this.setVisible(true);
//璁剧疆绐楀彛涓婄殑鍥炬爣
Image img = this.getToolkit().getImage(getClass().getResource("/image/Recorder.jpg"));
this.setIconImage(img);
//璁剧疆绐楀彛鍦ㄦ渶鍓嶇鏄剧ず
this.setAlwaysOnTop(true);
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
//褰撳紑濮嬪綍闊虫寜閽鎸変笅鏃跺氨寮€濮嬪綍闊?br> if(e.getSource().equals(captureBtn))
{
//鏀瑰彉鎸夐挳涓婇潰鐨勫瓧鐨勫唴瀹?br> captureBtn.setText("鏉惧紑 缁撴潫");
//璋冪敤褰曢煶鐨勬柟娉?br> capture();
//璁板綍寮€濮嬪綍闊崇殑鏃堕棿
startPlay = System.currentTimeMillis();
}
}
public void mouseReleased(MouseEvent e) {
//褰撴澗寮€褰曢煶鎸夐挳鏃跺仠姝㈠綍闊冲苟淇濆瓨褰曢煶鐨勬枃浠?br> if(e.getSource().equals(captureBtn))
{
//璋冪敤鍋滄褰曢煶鐨勬柟娉?br> stop();
//褰撴澗寮€鎸夐挳鍚庡鏄剧ず娉㈠舰鐨勯潰鏉胯繘琛屾竻绌?br> jp2.repaint();
//鏀瑰彉鎸夐挳涓婇潰鐨勫瓧鐨勫唴瀹?br> captureBtn.setText("鎸変綇 璇磋瘽");
//璋冪敤淇濆瓨褰曢煶鐨勬柟娉?br> save();
//灏嗗叾鏀惧埌鎸囧畾鐨勮矾寰勪笅
//瀹氫箟鏈€缁堣瀛樻斁鐨勬枃浠惰矾寰?br> String destPath = "D:/Program Files/apache-tomcat-6.0.35/webapps/XWZ/tempFile/";
copyFile("D:/"+tarFile.getName(), destPath);
System.exit(0);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//寮€濮嬪綍闊?br> public void capture()
{
try {
//af涓篈udioFormat涔熷氨鏄煶棰戞牸寮?br> af = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class,af);
td = (TargetDataLine)(AudioSystem.getLine(info));
//鎵撳紑鍏锋湁鎸囧畾鏍煎紡鐨勮锛岃繖鏍峰彲浣胯鑾峰緱鎵€鏈夋墍闇€鐨勭郴缁熻祫婧愬苟鍙樺緱鍙搷浣溿€?br> td.open(af);
//鍏佽鏌愪竴鏁版嵁琛屾墽琛屾暟鎹?nbsp;I/O
td.start();
//鍚姩鏄剧ず娉㈠舰鐨勮繘绋?br> RecordWave aw = new RecordWave();
Thread t2 = new Thread(aw);
t2.start();
//鎶婃樉绀烘尝褰㈢殑杩涚▼鏍囧織璁句负true
flag = true;
Record record = new Record();
Thread t1 = new Thread(record);
t1.start();
} catch (Exception ex) {
ex.printStackTrace();
return;
}
}
//鍋滄褰曢煶
public void stop()
{
stopflag = true;
//灏嗙敾娉㈠舰鐨勮繘绋嬬粓姝?br> flag = false;
}
//淇濆瓨褰曢煶
public void save()
{
af = getAudioFormat();
byte audioData[] = baos.toByteArray();
bais = new ByteArrayInputStream(audioData);
ais = new AudioInputStream(bais,af, audioData.length / af.getFrameSize());
//瀹氫箟鏈€缁堜繚瀛樼殑鏂囦欢鍚?br> File file = null;
//鍐欏叆鏂囦欢
try {
//浠ュ綋鍓嶇殑鏃堕棿鍛藉悕褰曢煶鐨勫悕瀛?br> //灏嗗綍闊崇殑鏂囦欢瀛樻斁鍒癋鐩樹笅璇煶鏂囦欢澶逛笅
File filePath = new File("D:/AudioFile");
String tarPath = "D:/";
if(!filePath.exists())
{//濡傛灉鏂囦欢涓嶅瓨鍦紝鍒欏垱寤鸿鐩綍
filePath.mkdirs();
}
long time = System.currentTimeMillis();
file = new File(filePath+"/"+time+".wav");
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, file);
//灏嗗綍闊充骇鐢熺殑wav鏂囦欢杞崲涓哄閲忚緝灏忕殑mp3鏍煎紡
//瀹氫箟浜х敓鍚庢枃浠跺悕
tarFile = new File(tarPath+time+".mp3");
Runtime run = null;
//娴嬭瘯褰撳墠鐨勮矾寰?br>
try {
run = Runtime.getRuntime();
//璋冪敤缂栫爜鍣ㄦ潵灏唚av鏂囦欢杞崲涓簃p3鏂囦欢
//鎶婄紪鐮佸緱鍒扮殑mp3鏂囦欢鍏堝瓨鏀惧埌D鐩樹笅锛岀劧鍚庡埄鐢ㄦ枃浠舵嫹璐濆嚱鏁板皢瀹冩斁鍒版寚瀹氱殑鏂囦欢澶逛笅鍚屾椂灏咲鐩樹笅鐨勬枃浠跺垹闄?br> Process p=run.exec(filePath+"/"+"lame -b 16 "+filePath+"/"+file.getName()+" "+tarPath+tarFile.getName()); //16涓虹爜鐜囷紝鍙嚜琛屼慨鏀?br> //閲婃斁杩涚▼
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
//绛夊緟
p.waitFor();
// //鍒犻櫎涔嬪墠淇濆瓨鐨勭殑wav鏂囦欢
// if(file.exists())
// {
// file.delete();
// }
// //瀹氫箟鏈€缁堣瀛樻斁鐨勬枃浠惰矾寰?br>// String destPath = "D:/Program Files/apache-tomcat-6.0.35/webapps/XWZ/tempFile/";
// copyFile(tarPath+tarFile.getName(), destPath);
} catch (Exception e) {
e.printStackTrace();
}finally{
//鏈€鍚庨兘瑕佹墽琛岀殑璇彞
//run璋冪敤lame瑙g爜鍣ㄦ渶鍚庨噴鏀惧唴瀛?br> run.freeMemory();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//鍏抽棴娴?br> try {
if(bais != null)
{
bais.close();
}
if(ais != null)
{
ais.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//鏂囦欢鎷疯礉鏂规硶
public void copyFile(String srcPath , String destPath)
{
File srcFile = new File(srcPath);
//濡傛灉鐩殑鏂囦欢澶规病鏈夊垯鍒涘缓鐩殑鏂囦欢澶?br> (new File(destPath)).mkdirs();
//鍦ㄧ洰鐨勬枃浠跺す涓嬪垱寤鸿澶嶅埗鐨勬枃浠?br> File destFile = new File(destPath+"/"+srcFile.getName());
if(srcFile.isFile() && srcFile.exists())
{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
//璁剧疆缂撳啿鏁扮粍
byte[] buff = new byte[1024*5];
int len = 0;
while ((len = in.read(buff)) != -1)
{
out.write(buff, 0, len);
}
// //娴嬭瘯璇ュ嚱鏁版槸鍚︽墽琛?br>// System.out.println("ok1");
} catch(Exception e) {
e.printStackTrace();
}finally{
//鍏抽棴娴侊紝鍏堝紑鐨勫悗鍏抽棴
try {
if(out != null)
{
out.close();
}
if(in != null)
{
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//澶嶅埗杩囧悗鍒犻櫎婧愭枃浠跺す涓殑鐨勬枃浠?br> if(srcFile.exists())
{
srcFile.delete();
}
}
//璁剧疆AudioFormat鐨勫弬鏁?br> public AudioFormat getAudioFormat()
{
//涓嬮潰娉ㄩ噴閮ㄥ垎鏄彟澶栦竴绉嶉煶棰戞牸寮忥紝涓よ€呴兘鍙互
AudioFormat.Encoding encoding = AudioFormat.Encoding.
PCM_SIGNED ;
float rate = 8000f;
int sampleSize = 16;
String signedString = "signed";
boolean bigEndian = true;
int channels = 1;
return new AudioFormat(encoding, rate, sampleSize, channels,
(sampleSize / 8) * channels, rate, bigEndian);
// //閲囨牱鐜囨槸姣忕鎾斁鍜屽綍鍒剁殑鏍锋湰鏁?br>// float sampleRate = 16000.0F;
// // 閲囨牱鐜?000,11025,16000,22050,44100
// //sampleSizeInBits琛ㄧず姣忎釜鍏锋湁姝ゆ牸寮忕殑澹伴煶鏍锋湰涓殑浣嶆暟
// int sampleSizeInBits = 16;
// // 8,16
// int channels = 1;
// // 鍗曞0閬撲负1锛岀珛浣撳0涓?
// boolean signed = true;
// // true,false
// boolean bigEndian = true;
// // true,false
// return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,bigEndian);
}
//褰曢煶绫伙紝鍥犱负瑕佺敤鍒癕yRecord绫讳腑鐨勫彉閲忥紝鎵€浠ュ皢鍏跺仛鎴愬唴閮ㄧ被
class Record implements Runnable
{
//瀹氫箟瀛樻斁褰曢煶鐨勫瓧鑺傛暟缁?浣滀负缂撳啿鍖?br> byte bts[] = new byte[10000];
//灏嗗瓧鑺傛暟缁勫寘瑁呭埌娴侀噷锛屾渶缁堝瓨鍏ュ埌baos涓?br> //閲嶅啓run鍑芥暟
public void run() {
baos = new ByteArrayOutputStream();
try {
stopflag = false;
while(stopflag != true)
{
//褰撳仠姝㈠綍闊虫病鎸変笅鏃讹紝璇ョ嚎绋嬩竴鐩存墽琛?nbsp;
//浠庢暟鎹鐨勮緭鍏ョ紦鍐插尯璇诲彇闊抽鏁版嵁銆?br> //瑕佽鍙朾ts.length闀垮害鐨勫瓧鑺?cnt 鏄疄闄呰鍙栫殑瀛楄妭鏁?br> int cnt = td.read(bts, 0, bts.length);
if(cnt > 0)
{
baos.write(bts, 0, cnt);
}
//寮€濮嬩粠闊抽娴佷腑璇诲彇瀛楄妭鏁?br> byte copyBts[] = bts;
bais = new ByteArrayInputStream(copyBts);
ais = new AudioInputStream(bais, af, copyBts.length/af.getFrameSize());
try{
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, af);
sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sd.open(af);
sd.start();
//浠庨煶棰戞祦涓鍙?br> int Buffer_Size = 10000;
audioDataBuffer = new byte[Buffer_Size];
int outBytes;
intBytes = ais.read(audioDataBuffer, 0,audioDataBuffer.length);
// 涓嶅啓鍒版贩棰戝櫒涓繖鏍峰氨涓嶄細鎾斁
// if (intBytes >= 0) {
// outBytes = sd.write(audioDataBuffer, 0,audioDataBuffer.length);
// }
}catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//intBytes = -1;
//鍏抽棴鎵撳紑鐨勫瓧鑺傛暟缁勬祦
if(baos != null)
{
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//涓嬮潰杩欏彞td.drain()涓嶈兘瑕侊紝杩欐牱濡傛灉涓嶆挱鏀炬暟鎹氨闃诲鍐嶆褰曢煶浼氬嚭鐜板叾浠栫▼搴忚闂敊璇?br> //td.drain();
td.close();
//鍒锋柊鏄剧ず娉㈠舰鐨勯潰鏉?br> jp2.repaint();
}
}
}
}
//鐢绘尝褰㈢殑绫?br> //鍥犱负瑕佷娇鐢ㄤ竴浜涗富鍑芥暟涓殑鏁版嵁锛屾墍浠ュ仛鎴愬唴閮ㄧ被
class RecordWave extends JPanel implements Runnable
{
//鐢ㄧ敾绗旂敾鍑烘尝褰?br> public void paint(Graphics g)
{
super.paint(g);
g.fillRect(jp2.getX(),jp2.getY() , 800, 380);
if( audioDataBuffer != null)
{
g.drawLine(jp2.getWidth() / 256, 700, jp2.getWidth() / 256, 700);
for(int i=0; i<audioDataBuffer.length-1; ++i)
{
g.setColor(Color.RED);
g.drawLine(i * jp2.getWidth() / 256, (int)audioDataBuffer[i]+200 , (i + 1)
* jp2.getWidth() / 256, (int)audioDataBuffer[i+1]+200);
}
}
}
public void run()
{
//鍒锋柊娉㈠舰
while(true)
{
//System.out.println("ok");
try {
synchronized (this) {
//闅斿闀挎椂闂磋幏鍙?br> Thread.sleep(300);
}
} catch (Exception e) {
e.printStackTrace();
}
this.paint(jp2.getGraphics());
//缁堟绾跨▼
if(flag == false)
{
break;
}
}
}
}
}
鈥斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€斺€?/span>
鐗堟潈澹版槑锛氭湰鏂囦负CSDN鍗氫富銆宩sjliuyun銆嶇殑鍘熷垱鏂囩珷锛岄伒寰狢C 4.0 BY-SA鐗堟潈鍗忚锛岃浆杞借闄勪笂鍘熸枃鍑哄閾炬帴鍙婃湰澹版槑銆?/span>
鍘熸枃閾炬帴锛?/span>
https://blog.csdn.net/computer_liuyun/article/details/17468555
绮変笣绂忓埄锛欽ava浠庡叆闂ㄥ埌鍏ュ湡瀛︿範璺嚎鍥?/span>
馃憞馃憞馃憞
馃憜闀挎寜涓婃柟寰俊浜岀淮鐮?nbsp;2 绉?/section>
鎰熻阿鐐硅禐鏀寔涓嬪搱