码迷,mamicode.com
首页 > 移动开发 > 详细

android麦克风自录自放demo

时间:2015-01-26 22:29:18      阅读:667      评论:0      收藏:0      [点我收藏+]

标签:

extends:http://blog.csdn.net/trbbadboy/article/details/7865530

是一个直接播放麦克风采集到的声音线程类:

  

  class RecordThread extends Thread{  
    static final int frequency = 44100;  
    static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
    static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;  
@Override  
public void run() {  
    // TODO Auto-generated method stub  
    int recBufSize = AudioRecord.getMinBufferSize(frequency,  
            channelConfiguration, audioEncoding)*2;  
    int plyBufSize = AudioTrack.getMinBufferSize(frequency,  
            channelConfiguration, audioEncoding)*2;  
      
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,  
            channelConfiguration, audioEncoding, recBufSize);  
  
    AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,  
            channelConfiguration, audioEncoding, plyBufSize, AudioTrack.MODE_STREAM);  
      
    byte[] recBuf = new byte[recBufSize];  
    audioRecord.startRecording();  
    audioTrack.play();  
    while(true){  
        int readLen = audioRecord.read(recBuf, 0, recBufSize);  
        audioTrack.write(recBuf, 0, readLen);  
    }  
    audioTrack.stop();  
    audioRecord.stop();  
}  
  }  
 

 

 

使用的时候,这样就行:

  

RecordThread rec = new RecordThread();  
rec.start();  

 

需要在AndroidManifest.xml文件中添加路由权限:

   

<uses-permission android:name="android.permission.RECORD_AUDIO"/>  

 

android麦克风自录自放demo

标签:

原文地址:http://www.cnblogs.com/niray/p/4251409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!