Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 3.58 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Nexus S 4g Audio recorder frequency
  2. package receive.ultrasound;
  3.  
  4. import java.io.BufferedOutputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import android.app.Activity;
  12. import android.media.AudioFormat;
  13. import android.media.AudioRecord;
  14. import android.media.MediaRecorder;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.os.Handler;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.widget.Button;
  22. import android.widget.Toast;
  23.  
  24. public class Beep_Beep_ReceiverActivity extends Activity{
  25.     private int min_buff;
  26.     private int len=0;
  27.  
  28.     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/demo.pcm");
  29.  
  30.     AudioRecord record = null;
  31.  
  32.      Handler handler = new Handler();
  33.  
  34.     @Override
  35.     public void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.main);
  38.  
  39.  
  40.         final Button buttonStart = (Button) findViewById(R.id.button1);
  41.         final Button buttonStop = (Button) findViewById(R.id.button2);
  42.  
  43.         buttonStart.setOnClickListener(new View.OnClickListener() {
  44.         public void onClick(View v) {
  45.             Toast.makeText(getApplicationContext(), "button1 clicked!", 50).show();
  46.             startDownload();
  47.         }
  48.     });
  49.  
  50.         buttonStop.setOnClickListener(new View.OnClickListener() {
  51.             public void onClick(View v) {
  52.                 Toast.makeText(getApplicationContext(), "button1 clicked!", 50).show();
  53.                 stopDownload();
  54.             }
  55.         });
  56.  
  57.     }
  58.  
  59.    #start download function:
  60.  
  61.     public void startDownload(){
  62.  
  63.         OutputStream os = null;
  64.         try {
  65.             os = new FileOutputStream(file);
  66.         } catch (FileNotFoundException e) {
  67.             // TODO Auto-generated catch block
  68.             e.printStackTrace();
  69.         }
  70.                 DataOutputStream dos = new DataOutputStream(os);
  71.  
  72.         try {
  73.             /*       Create a DataOuputStream to write the audio data into the saved file.*/
  74.  
  75.  
  76.  
  77.  
  78.                   min_buff = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
  79.                         AudioFormat.ENCODING_PCM_16BIT);
  80.  
  81.                   if( min_buff == AudioRecord.ERROR_BAD_VALUE )
  82.                         Log.e( "ram", "hardware not supported for this configuration" );
  83.  
  84.                   if( min_buff == AudioRecord.ERROR )
  85.                         Log.e( "ram", "Bad Value for "minBufferSize", implementation was unable to query the hardware for its output properties" );
  86.  
  87.  
  88.      #This is where i give the sample rate.            
  89.  
  90.          AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
  91.                         AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, min_buff);
  92.  
  93.  
  94.  
  95.  
  96.  
  97.                   byte[] buffer = new byte[min_buff * 10];
  98.                   byte[] temp_buffer = new byte[min_buff];
  99.  
  100.                   record.startRecording();
  101.  
  102.  
  103.                   while (len < (min_buff * 10)) {
  104.                          int bufferReadResult = record.read(temp_buffer, 0, min_buff);
  105.                          for (int i = 0; i < bufferReadResult; i++)
  106.                                     dos.writeByte(temp_buffer[i]);
  107.                          len += min_buff;
  108.                   }
  109.  
  110.  
  111.               //    record.stop();
  112.               //  dos.close();
  113.         }catch (Exception e) {
  114.  
  115.         }
  116.  
  117.  
  118.     }
  119.  
  120.     public void stopDownload(){
  121.  
  122.        System.exit(0);      
  123.     }
  124.  
  125. }