Advertisement
Guest User

Android recorder

a guest
Jul 7th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1.  
  2. public class RecordTask implements Runnable{
  3.    
  4.     public static final int SAMPLERATE = 44100;
  5.     public static final int SOURCE = MediaRecorder.AudioSource.MIC;
  6.     public static final int CHANELS = AudioFormat.CHANNEL_IN_MONO;
  7.     public static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
  8.     public static final int DEFAULT_BLOCKSIZE = 1024;
  9.     private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
  10.     public AudioRecord findAudioRecord() {
  11.         for (int rate : mSampleRates) {
  12.             for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
  13.                 for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
  14.                     try {
  15.                     //    Log.d(C.TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
  16.                      //           + channelConfig);
  17.                         int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
  18.  
  19.                         if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
  20.                             // check if we can instantiate and have a success
  21.                             AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
  22.  
  23.                             if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
  24.                                 return recorder;
  25.                         }
  26.                     } catch (Exception e) {
  27.                       //  Log.e(C.TAG, rate + "Exception, keep trying.",e);
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.         return null;
  33.     }
  34.    
  35.     private AudioRecord recorder;  
  36.    
  37.     private LinkedBlockingDeque<short[]> queueData;
  38.    
  39.     private int block_size;
  40.    
  41.     private boolean isRecording;
  42.        
  43.     public LinkedBlockingDeque<short[]> getQueueData() {
  44.         return queueData;
  45.     }
  46.  
  47.     public void setQueueData(LinkedBlockingDeque<short[]> queueData) {
  48.         this.queueData = queueData;
  49.     }
  50.    
  51.     public int getBlockSize() {
  52.         return block_size;
  53.     }
  54.    
  55.     private void init(LinkedBlockingDeque<short[]> queue,int block_size)
  56.     {
  57.         this.queueData = queue;
  58.         this.block_size = block_size;
  59.         this.isRecording = false;
  60.         this.recorder = new AudioRecord(SOURCE, SAMPLERATE, CHANELS, ENCODING, 8192);
  61.     }
  62.    
  63.     public RecordTask()
  64.     {
  65.         init(new LinkedBlockingDeque<short[]>(), DEFAULT_BLOCKSIZE);
  66.     }
  67.     public RecordTask(LinkedBlockingDeque<short[]> queue,int block_size)
  68.     {
  69.         init(queue,block_size);
  70.     }
  71.     public RecordTask(LinkedBlockingDeque<short[]> queue)
  72.     {
  73.         init(queue,DEFAULT_BLOCKSIZE);
  74.     }
  75.    
  76.     @Override
  77.     public void run() {
  78.         recorder.startRecording();
  79.         isRecording = true;
  80.         while(isRecording)
  81.         {
  82.             short[] buf = new short[block_size];
  83.             int numShort = recorder.read(buf, 0, block_size);
  84.            
  85.             if(numShort>0)
  86.             {
  87.                 queueData.add(buf);
  88.             }
  89.             else
  90.             {
  91.                 break;
  92.             }
  93.         }
  94.        
  95.         recorder.stop();
  96.         //recorder.release();
  97.     }
  98.  
  99.     public void stopRecording()
  100.     {
  101.         this.isRecording = false;
  102.     }
  103.    
  104.     public void reset(){
  105.         this.isRecording = true;
  106.         this.queueData.clear();
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement