android 录音写成wav文件格式数据

在Android中录音可以用MediaRecord录音,操作比较简单。但是不够专业,就是不能对音频进行处理。如果要进行音频的实时的处理或者音频的一些封装

就可以用AudioRecord来进行录音了。

这里给出一段代码。实现了AudioRecord的录音和WAV格式音频的封装。

用AudioTrack和AudioTrack类可以进行边录边播,可以参考:http://blog.sina.com.cn/s/blog_6309e1ed0100j1rw.html

我们这里的代码没有播放。但是有封装和详解,如下:

[java]  view plain copy print ?
  1. package com.ppmeet;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import android.app.Activity;  
  9. import android.graphics.PixelFormat;  
  10. import android.media.AudioFormat;  
  11. import android.media.AudioRecord;  
  12. import android.media.MediaRecorder;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.view.Window;  
  17. import android.view.WindowManager;  
  18. import android.widget.Button;  
  19.   
  20. /** 
  21.  * class name:TestAudioRecord<BR> 
  22.  * class description:用AudioRecord来进行录音<BR> 
  23.  * PS: <BR> 
  24.  *  
  25.  * @version 1.00 2011/09/21 
  26.  * @author CODYY)peijiangping 
  27.  */  
  28. public class TestAudioRecord extends Activity {  
  29.     // 音频获取源  
  30.     private int audioSource = MediaRecorder.AudioSource.MIC;  
  31.     // 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025  
  32.     private static int sampleRateInHz = 44100;  
  33.     // 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道  
  34.     private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;  
  35.     // 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。  
  36.     private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;  
  37.     // 缓冲区字节大小  
  38.     private int bufferSizeInBytes = 0;  
  39.     private Button Start;  
  40.     private Button Stop;  
  41.     private AudioRecord audioRecord;  
  42.     private boolean isRecord = false;// 设置正在录制的状态  
  43.     //AudioName裸音频数据文件  
  44.     private static final String AudioName = "/sdcard/love.raw";  
  45.     //NewAudioName可播放的音频文件  
  46.     private static final String NewAudioName = "/sdcard/new.wav";  
  47.   
  48.     public void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         getWindow().setFormat(PixelFormat.TRANSLUCENT);// 让界面横屏  
  51.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉界面标题  
  52.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  53.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  54.         // 重新设置界面大小  
  55.         setContentView(R.layout.main);  
  56.         init();  
  57.     }  
  58.   
  59.     private void init() {  
  60.         Start = (Button) this.findViewById(R.id.start);  
  61.         Stop = (Button) this.findViewById(R.id.stop);  
  62.         Start.setOnClickListener(new TestAudioListener());  
  63.         Stop.setOnClickListener(new TestAudioListener());  
  64.         creatAudioRecord();  
  65.     }  
  66.   
  67.     private void creatAudioRecord() {  
  68.         // 获得缓冲区字节大小  
  69.         bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,  
  70.                 channelConfig, audioFormat);  
  71.         // 创建AudioRecord对象  
  72.         audioRecord = new AudioRecord(audioSource, sampleRateInHz,  
  73.                 channelConfig, audioFormat, bufferSizeInBytes);  
  74.     }  
  75.   
  76.     class TestAudioListener implements OnClickListener {  
  77.   
  78.         @Override  
  79.         public void onClick(View v) {  
  80.             if (v == Start) {  
  81.                 startRecord();  
  82.             }  
  83.             if (v == Stop) {  
  84.                 stopRecord();  
  85.             }  
  86.   
  87.         }  
  88.   
  89.     }  
  90.   
  91.     private void startRecord() {  
  92.         audioRecord.startRecording();  
  93.         // 让录制状态为true  
  94.         isRecord = true;  
  95.         // 开启音频文件写入线程  
  96.         new Thread(new AudioRecordThread()).start();  
  97.     }  
  98.   
  99.     private void stopRecord() {  
  100.         close();  
  101.     }  
  102.   
  103.     private void close() {  
  104.         if (audioRecord != null) {  
  105.             System.out.println("stopRecord");  
  106.             isRecord = false;//停止文件写入  
  107.             audioRecord.stop();  
  108.             audioRecord.release();//释放资源  
  109.             audioRecord = null;  
  110.         }  
  111.     }  
  112.   
  113.     class AudioRecordThread implements Runnable {  
  114.         @Override  
  115.         public void run() {  
  116.             writeDateTOFile();//往文件中写入裸数据  
  117.             copyWaveFile(AudioName, NewAudioName);//给裸数据加上头文件  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * 这里将数据写入文件,但是并不能播放,因为AudioRecord获得的音频是原始的裸音频, 
  123.      * 如果需要播放就必须加入一些格式或者编码的头信息。但是这样的好处就是你可以对音频的 裸数据进行处理,比如你要做一个爱说话的TOM 
  124.      * 猫在这里就进行音频的处理,然后重新封装 所以说这样得到的音频比较容易做一些音频的处理。 
  125.      */  
  126.     private void writeDateTOFile() {  
  127.         // new一个byte数组用来存一些字节数据,大小为缓冲区大小  
  128.         byte[] audiodata = new byte[bufferSizeInBytes];  
  129.         FileOutputStream fos = null;  
  130.         int readsize = 0;  
  131.         try {  
  132.             File file = new File(AudioName);  
  133.             if (file.exists()) {  
  134.                 file.delete();  
  135.             }  
  136.             fos = new FileOutputStream(file);// 建立一个可存取字节的文件  
  137.         } catch (Exception e) {  
  138.             e.printStackTrace();  
  139.         }  
  140.         while (isRecord == true) {  
  141.             readsize = audioRecord.read(audiodata, 0, bufferSizeInBytes);  
  142.             if (AudioRecord.ERROR_INVALID_OPERATION != readsize) {  
  143.                 try {  
  144.                     fos.write(audiodata);  
  145.                 } catch (IOException e) {  
  146.                     e.printStackTrace();  
  147.                 }  
  148.             }  
  149.         }  
  150.         try {  
  151.             fos.close();// 关闭写入流  
  152.         } catch (IOException e) {  
  153.             e.printStackTrace();  
  154.         }  
  155.     }  
  156.   
  157.     // 这里得到可播放的音频文件  
  158.     private void copyWaveFile(String inFilename, String outFilename) {  
  159.         FileInputStream in = null;  
  160.         FileOutputStream out = null;  
  161.         long totalAudioLen = 0;  
  162.         long totalDataLen = totalAudioLen + 36;  
  163.         long longSampleRate = sampleRateInHz;  
  164.         int channels = 2;  
  165.         long byteRate = 16 * sampleRateInHz * channels / 8;  
  166.         byte[] data = new byte[bufferSizeInBytes];  
  167.         try {  
  168.             in = new FileInputStream(inFilename);  
  169.             out = new FileOutputStream(outFilename);  
  170.             totalAudioLen = in.getChannel().size();  
  171.             totalDataLen = totalAudioLen + 36;  
  172.             WriteWaveFileHeader(out, totalAudioLen, totalDataLen,  
  173.                     longSampleRate, channels, byteRate);  
  174.             while (in.read(data) != -1) {  
  175.                 out.write(data);  
  176.             }  
  177.             in.close();  
  178.             out.close();  
  179.         } catch (FileNotFoundException e) {  
  180.             e.printStackTrace();  
  181.         } catch (IOException e) {  
  182.             e.printStackTrace();  
  183.         }  
  184.     }  
  185.   
  186.     /** 
  187.      * 这里提供一个头信息。插入这些信息就可以得到可以播放的文件。 
  188.      * 为我为啥插入这44个字节,这个还真没深入研究,不过你随便打开一个wav 
  189.      * 音频的文件,可以发现前面的头文件可以说基本一样哦。每种格式的文件都有 
  190.      * 自己特有的头文件。 
  191.      */  
  192.     private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,  
  193.             long totalDataLen, long longSampleRate, int channels, long byteRate)  
  194.             throws IOException {  
  195.         byte[] header = new byte[44];  
  196.         header[0] = 'R'// RIFF/WAVE header  
  197.         header[1] = 'I';  
  198.         header[2] = 'F';  
  199.         header[3] = 'F';  
  200.         header[4] = (byte) (totalDataLen & 0xff);  
  201.         header[5] = (byte) ((totalDataLen >> 8) & 0xff);  
  202.         header[6] = (byte) ((totalDataLen >> 16) & 0xff);  
  203.         header[7] = (byte) ((totalDataLen >> 24) & 0xff);  
  204.         header[8] = 'W';  
  205.         header[9] = 'A';  
  206.         header[10] = 'V';  
  207.         header[11] = 'E';  
  208.         header[12] = 'f'// 'fmt ' chunk  
  209.         header[13] = 'm';  
  210.         header[14] = 't';  
  211.         header[15] = ' ';  
  212.         header[16] = 16// 4 bytes: size of 'fmt ' chunk  
  213.         header[17] = 0;  
  214.         header[18] = 0;  
  215.         header[19] = 0;  
  216.         header[20] = 1// format = 1  
  217.         header[21] = 0;  
  218.         header[22] = (byte) channels;  
  219.         header[23] = 0;  
  220.         header[24] = (byte) (longSampleRate & 0xff);  
  221.         header[25] = (byte) ((longSampleRate >> 8) & 0xff);  
  222.         header[26] = (byte) ((longSampleRate >> 16) & 0xff);  
  223.         header[27] = (byte) ((longSampleRate >> 24) & 0xff);  
  224.         header[28] = (byte) (byteRate & 0xff);  
  225.         header[29] = (byte) ((byteRate >> 8) & 0xff);  
  226.         header[30] = (byte) ((byteRate >> 16) & 0xff);  
  227.         header[31] = (byte) ((byteRate >> 24) & 0xff);  
  228.         header[32] = (byte) (2 * 16 / 8); // block align  
  229.         header[33] = 0;  
  230.         header[34] = 16// bits per sample  
  231.         header[35] = 0;  
  232.         header[36] = 'd';  
  233.         header[37] = 'a';  
  234.         header[38] = 't';  
  235.         header[39] = 'a';  
  236.         header[40] = (byte) (totalAudioLen & 0xff);  
  237.         header[41] = (byte) ((totalAudioLen >> 8) & 0xff);  
  238.         header[42] = (byte) ((totalAudioLen >> 16) & 0xff);  
  239.         header[43] = (byte) ((totalAudioLen >> 24) & 0xff);  
  240.         out.write(header, 044);  
  241.     }  
  242.   
  243.     @Override  
  244.     protected void onDestroy() {  
  245.         close();  
  246.         super.onDestroy();  
  247.     }  
  248. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值