在Android中录音可以用MediaRecord录音,操作比较简单。但是不够专业,就是不能对音频进行处理。如果要进行音频的实时的处理或者音频的一些封装
就可以用AudioRecord来进行录音了。
这里给出一段代码。实现了AudioRecord的录音和WAV格式音频的封装。
用AudioTrack和AudioTrack类可以进行边录边播,可以参考:http://blog.sina.com.cn/s/blog_6309e1ed0100j1rw.html
我们这里的代码没有播放。但是有封装和详解,如下:
- packagecom.ppmeet;
-
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importandroid.app.Activity;
- importandroid.graphics.PixelFormat;
- importandroid.media.AudioFormat;
- importandroid.media.AudioRecord;
- importandroid.media.MediaRecorder;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.Window;
- importandroid.view.WindowManager;
- importandroid.widget.Button;
-
-
-
-
-
-
-
-
-
- publicclassTestAudioRecordextendsActivity{
-
- privateintaudioSource=MediaRecorder.AudioSource.MIC;
-
- privatestaticintsampleRateInHz=44100;
-
- privatestaticintchannelConfig=AudioFormat.CHANNEL_IN_STEREO;
-
- privatestaticintaudioFormat=AudioFormat.ENCODING_PCM_16BIT;
-
- privateintbufferSizeInBytes=0;
- privateButtonStart;
- privateButtonStop;
- privateAudioRecordaudioRecord;
- privatebooleanisRecord=false;
-
- privatestaticfinalStringAudioName="/sdcard/love.raw";
-
- privatestaticfinalStringNewAudioName="/sdcard/new.wav";
-
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.main);
- init();
- }
-
- privatevoidinit(){
- Start=(Button)this.findViewById(R.id.start);
- Stop=(Button)this.findViewById(R.id.stop);
- Start.setOnClickListener(newTestAudioListener());
- Stop.setOnClickListener(newTestAudioListener());
- creatAudioRecord();
- }
-
- privatevoidcreatAudioRecord(){
-
- bufferSizeInBytes=AudioRecord.getMinBufferSize(sampleRateInHz,
- channelConfig,audioFormat);
-
- audioRecord=newAudioRecord(audioSource,sampleRateInHz,
- channelConfig,audioFormat,bufferSizeInBytes);
- }
-
- classTestAudioListenerimplementsOnClickListener{
-
- @Override
- publicvoidonClick(Viewv){
- if(v==Start){
- startRecord();
- }
- if(v==Stop){
- stopRecord();
- }
-
- }
-
- }
-
- privatevoidstartRecord(){
- audioRecord.startRecording();
-
- isRecord=true;
-
- newThread(newAudioRecordThread()).start();
- }
-
- privatevoidstopRecord(){
- close();
- }
-
- privatevoidclose(){
- if(audioRecord!=null){
- System.out.println("stopRecord");
- isRecord=false;
- audioRecord.stop();
- audioRecord.release();
- audioRecord=null;
- }
- }
-
- classAudioRecordThreadimplementsRunnable{
- @Override
- publicvoidrun(){
- writeDateTOFile();
- copyWaveFile(AudioName,NewAudioName);
- }
- }
-
-
-
-
-
-
- privatevoidwriteDateTOFile(){
-
- byte[]audiodata=newbyte[bufferSizeInBytes];
- FileOutputStreamfos=null;
- intreadsize=0;
- try{
- Filefile=newFile(AudioName);
- if(file.exists()){
- file.delete();
- }
- fos=newFileOutputStream(file);
- }catch(Exceptione){
- e.printStackTrace();
- }
- while(isRecord==true){
- readsize=audioRecord.read(audiodata,0,bufferSizeInBytes);
- if(AudioRecord.ERROR_INVALID_OPERATION!=readsize){
- try{
- fos.write(audiodata);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }
- try{
- fos.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
-
-
- privatevoidcopyWaveFile(StringinFilename,StringoutFilename){
- FileInputStreamin=null;
- FileOutputStreamout=null;
- longtotalAudioLen=0;
- longtotalDataLen=totalAudioLen+36;
- longlongSampleRate=sampleRateInHz;
- intchannels=2;
- longbyteRate=16*sampleRateInHz*channels/8;
- byte[]data=newbyte[bufferSizeInBytes];
- try{
- in=newFileInputStream(inFilename);
- out=newFileOutputStream(outFilename);
- totalAudioLen=in.getChannel().size();
- totalDataLen=totalAudioLen+36;
- WriteWaveFileHeader(out,totalAudioLen,totalDataLen,
- longSampleRate,channels,byteRate);
- while(in.read(data)!=-1){
- out.write(data);
- }
- in.close();
- out.close();
- }catch(FileNotFoundExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
- privatevoidWriteWaveFileHeader(FileOutputStreamout,longtotalAudioLen,
- longtotalDataLen,longlongSampleRate,intchannels,longbyteRate)
- throwsIOException{
- byte[]header=newbyte[44];
- header[0]='R';
- header[1]='I';
- header[2]='F';
- header[3]='F';
- header[4]=(byte)(totalDataLen&0xff);
- header[5]=(byte)((totalDataLen>>8)&0xff);
- header[6]=(byte)((totalDataLen>>16)&0xff);
- header[7]=(byte)((totalDataLen>>24)&0xff);
- header[8]='W';
- header[9]='A';
- header[10]='V';
- header[11]='E';
- header[12]='f';
- header[13]='m';
- header[14]='t';
- header[15]='';
- header[16]=16;
- header[17]=0;
- header[18]=0;
- header[19]=0;
- header[20]=1;
- header[21]=0;
- header[22]=(byte)channels;
- header[23]=0;
- header[24]=(byte)(longSampleRate&0xff);
- header[25]=(byte)((longSampleRate>>8)&0xff);
- header[26]=(byte)((longSampleRate>>16)&0xff);
- header[27]=(byte)((longSampleRate>>24)&0xff);
- header[28]=(byte)(byteRate&0xff);
- header[29]=(byte)((byteRate>>8)&0xff);
- header[30]=(byte)((byteRate>>16)&0xff);
- header[31]=(byte)((byteRate>>24)&0xff);
- header[32]=(byte)(2*16/8);
- header[33]=0;
- header[34]=16;
- header[35]=0;
- header[36]='d';
- header[37]='a';
- header[38]='t';
- header[39]='a';
- header[40]=(byte)(totalAudioLen&0xff);
- header[41]=(byte)((totalAudioLen>>8)&0xff);
- header[42]=(byte)((totalAudioLen>>16)&0xff);
- header[43]=(byte)((totalAudioLen>>24)&0xff);
- out.write(header,0,44);
- }
-
- @Override
- protectedvoidonDestroy(){
- close();
- super.onDestroy();
- }
- }
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<wbr><wbr><wbr>AudioRecord和AudioTrack类是Android获取和播放音频流的重要类,放置在android.media包中。与该包中的MediaRecorder和MediaPlayer类不同,AudioRecord和AudioTrack类在获取和播放音频数据流时无需通过文件保存和文件读取,可以动态地直接获取和播放音频流,在实时处理音频数据流时非常有用。</wbr></wbr></wbr>
<wbr><wbr><wbr>当然,如果用户只想录音后写入文件或从文件中取得音频流进行播放,那么直接使用MediaRecorder和MediaPlayer类是首选方案,因为这两个类使用非常方便,而且成功率很高。而AudioRecord和AudioTrack类的使用却比较复杂,我们发现很多人都不能成功地使用这两个类,甚至认为Android的这两个类是不能工作的。</wbr></wbr></wbr>
<wbr><wbr><wbr>其实,AudioRecord和AudioTrack类的使用虽然比较复杂,但是可以工作,我们不仅可以很好地使用了这两个类,而且还通过套接字(Socket)实现了音频数据的网络传输,做到了一端使用AudioRecord获取音频流然后通过套接字传输出去,而另一端通过套接字接收后使用AudioTrack类播放。</wbr></wbr></wbr>
<wbr><wbr><wbr>下面是我们对AudioRecord和AudioTrack类在使用方面的经验总结:</wbr></wbr></wbr>
<wbr><wbr><wbr>(1)创建AudioRecord和AudioTrack类对象:创建这两个类的对象比较复杂,通过对文档的反复和仔细理解,并通过多次失败的尝试,并在北理工的某个Android大牛的网上的文章启发下,我们也最终成功地创建了这两个类的对象。创建<span style="word-wrap:normal; word-break:normal">AudioRecord和AudioTrack类对象的代码如下:</span></wbr></wbr></wbr>
AudioRecord类:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_buf_size =AudioRecord.getMinBufferSize(8000,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.CHANNEL_CONFIGURATION_MONO,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.ENCODING_PCM_16BIT);<br><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_rec = new AudioRecord(MediaRecorder.AudioSource.MIC,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>8000,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.CHANNEL_CONFIGURATION_MONO,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.ENCODING_PCM_16BIT,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_buf_size) ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
AudioTrack类:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_out_buf_size = android.media.AudioTrack.getMinBufferSize(8000,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.CHANNEL_CONFIGURATION_MONO,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.ENCODING_PCM_16BIT);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_out_trk = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.CHANNEL_CONFIGURATION_MONO,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioFormat.ENCODING_PCM_16BIT,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_out_buf_size,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>AudioTrack.MODE_STREAM);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal">(2)关于AudioRecord和AudioTrack类的监听函数,不用也行。</span></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr>(3)调试方面,包括初始化后看logcat信息,以确定类的工作状态,初始化是否成功等。</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>编写好代码,没有语法错误,调用模拟器运行、调试代码时,logcat发挥了很好的功用。刚调试时,经常会出现模拟器显示出现异常,这时我们可以在代码的一些关键语句后添加如Log.d("test1","OK");这样的语句进行标识,出现异常时我们就可以在logcat窗口观察代码执行到哪里出现异常,然后进行相应的修改、调试。模拟器不会出现异常时,又遇到了录放音的问题。录音方面,刚开始选择将语音编码数据存放在多个固定大小的文件中进行传送,但是这种情况下会出现声音断续的现象,而且要反复的建立文件,比较麻烦,后来想到要进行网上传输,直接将语音编码数据以数据流的形式传送,经过验证,这种方法可行并且使代码更加简洁。放音方面,将接收到的数据流存放在一个数组中,然后将数组中数据写到AudioTrack中。刚开始只是“嘟”几声,经过检查发现只是把数据写一次,加入循环,让数据反复写到AudioTrack中,就可以听到正常的语音了。接下来的工作主要是改善话音质量与话音延迟,在进行通话的过程中,观察</wbr></wbr></wbr></wbr></wbr>logcat窗口,发现向数组中写数据时会出现Bufferflow的情况,于是把重心转移到数组大小的影响上,经过试验,发现 AudioRecord一次会读640个数据,然后就对录音和放音中有数组的地方进行实验修改。AudioRecord和AudioTrack进行实例化时,参数中各有一个数组大小,经过试验这个数组大小和AudioRecord和AudioTrack能正常实例化所需的最小Buffer大小(即上面实例化时的m_in_buf_size和m_out_buf_size参数)相等且服务器方进行缓存数据的数组尺寸是上述数值的2倍时,语音质量最好。由于录音和放音的速度不一致,受到北理工大牛的启发,在录音方面,将存放录音数据的数组放到LinkedList中,当LinkedList中数组个数达到2(这个也是经过试验验证话音质量最好时的数据)时,将先录好的数组中数据传送出去。经过上述反复试验和修改,最终使双方通话质量较好,且延时较短(大概有2秒钟)。
<wbr><wbr><wbr>(4)通过套接字传输和接收数据</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>数据传送部分,使用的是套接字。通信双方,通过不同的端口向服务器发送请求,与服务器连接上后,开始通话向服务器发送数据,服务器通过一个套接字接收到一方的数据后,先存在一个数组中,然后将该数组中数据以数据流的形式再通过另一个套接字传送到另一方。这样就实现了双方数据的传送。</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>(5)代码架构</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>为避免反复录入和读取数据占用较多资源,使程序在进行录放音时不能执行其他命令,故将录音和放音各写成一个线程类,然后在主程序中,通过MENU控制通话的开始、停止、结束。</wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal">最后说明,AudioRecord和AudioTrack类可以用,只是稍微复杂些。以下贴出双方通信的源码,希望对大家有所帮助:</span></wbr></wbr></wbr></wbr></wbr>
主程序Daudioclient:
package cn.Daudioclient;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Daudioclient extends Activity {
<wbr><br><wbr><wbr><wbr><wbr>public static final int MENU_START_ID = Menu.FIRST ;<br><wbr><wbr><wbr>public static final int MENU_STOP_ID = Menu.FIRST + 1 ;<br><wbr><wbr><wbr>public static final int MENU_EXIT_ID = Menu.FIRST + 2 ;<br><wbr><br><wbr><wbr><wbr>protected Saudioserver<wbr><wbr><wbr><wbr>m_player ;<br><wbr><wbr><wbr>protected Saudioclient<wbr><wbr><wbr><wbr>m_recorder ;<br><wbr><br><wbr><wbr><wbr><br><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr>public void onCreate(Bundle savedInstanceState) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>super.onCreate(savedInstanceState);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>setContentView(R.layout.main);<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>public boolean onCreateOptionsMenu(Menu aMenu)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>boolean res = super.onCreateOptionsMenu(aMenu) ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>aMenu.add(0, MENU_START_ID, 0, "START") ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>aMenu.add(0, MENU_STOP_ID, 0, "STOP") ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>aMenu.add(0, MENU_EXIT_ID, 0, "EXIT") ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>return res ;<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><br><wbr><wbr><wbr>public boolean onOptionsItemSelected(MenuItem aMenuItem)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>switch (aMenuItem.getItemId()) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>case MENU_START_ID:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_player = new Saudioserver() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_recorder = new Saudioclient() ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_player.init() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_recorder.init() ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_recorder.start() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_player.start() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>case MENU_STOP_ID:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_recorder.free() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_player.free() ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_player = null ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_recorder = null ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>case MENU_EXIT_ID:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int pid = android.os.Process.myPid() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android.os.Process.killProcess(pid) ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>default:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>break ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>return super.onOptionsItemSelected(aMenuItem);<br><wbr><wbr><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
录音程序Saudioclient:
package cn.Daudioclient;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
public class Saudioclient extends Thread
{
<wbr><br><wbr><wbr><wbr>protected AudioRecord m_in_rec ;<wbr><br><wbr><wbr><wbr>protected int<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_buf_size ;<br><wbr><wbr><wbr>protected byte []<wbr><wbr><wbr><wbr>m_in_bytes ;<br><wbr><wbr><wbr>protected boolean<wbr><wbr><wbr><wbr>m_keep_running ;<br><wbr><wbr><wbr>protected Socket<wbr><wbr><wbr><wbr><wbr>s;<br><wbr><wbr><wbr>protected DataOutputStream dout;<br><wbr><wbr><wbr>protected LinkedList<byte[]><wbr>m_in_q ;<br><wbr><wbr><br><wbr><wbr><wbr>public void run()<br><wbr>{<br><wbr><wbr><wbr><wbr><wbr>try<br><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>byte [] bytes_pkg ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_rec.startRecording() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>while(m_keep_running)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_rec.read(m_in_bytes, 0, m_in_buf_size) ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>bytes_pkg = m_in_bytes.clone() ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(m_in_q.size() >= 2)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dout.write(m_in_q.removeFirst() , 0, m_in_q.removeFirst() .length);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_in_q.add(bytes_pkg) ;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>