1. Download our Official Android App: Forums for Android!

Apps Help with voice recording

Discussion in 'Android Development' started by Davidb93, Mar 17, 2011.

  1. Davidb93

    Davidb93 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Mar 17, 2011

    Mar 17, 2011
    1
    0
    5
    Hi all,
    I trying to write app that record voice and send it through tcp socket.
    Currently i succeed to record voice to sd card. My problem is that the recording buffer works only for short [] but in order to send it through the socket i should convert it to bytes. When i am trying to convert the short [] to byte i get only noise in the created file on sd card.
    Can some one help me to do it ??
    This is my code:
    Code (Text):
    1.  
    2.  
    3. import android.os.Bundle;
    4. import android.os.Environment;
    5. import android.app.Activity;
    6. import android.media.AudioFormat;
    7. import android.media.AudioRecord;
    8. import android.media.MediaRecorder;
    9.  
    10. import java.io.BufferedOutputStream;
    11. import java.io.DataOutputStream;
    12. import java.io.File;
    13. import java.io.FileNotFoundException;
    14. import java.io.FileOutputStream;
    15. import java.io.IOException;
    16.  
    17.  
    18. public class MainActivity extends Activity
    19. {
    20.     /** Called when the activity is first created. */
    21.     @Override
    22.     public void onCreate(Bundle savedInstanceState)
    23.     {
    24.         super.onCreate(savedInstanceState);
    25.         setContentView(R.layout.main);
    26.         Recorder recorderInstance = new Recorder();
    27.         Thread th = new Thread(recorderInstance);
    28.         recorderInstance.setFileName(
    29.                 new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my2.pcm"));
    30.         th.start();
    31.         recorderInstance.setRecording(true);
    32.         synchronized (this)
    33.         {
    34.             try
    35.             {              
    36.                 this.wait(20000);
    37.            
    38.             }
    39.             catch (InterruptedException e)
    40.             {
    41.                 e.printStackTrace();
    42.             }
    43.         }
    44.         recorderInstance.setRecording(false);
    45.         try
    46.         {
    47.             th.join();
    48.         } catch (InterruptedException e)
    49.         {
    50.             e.printStackTrace();
    51.            
    52.         }
    53.     }
    54. }
    55.  
    56.  
    57. class Recorder implements Runnable
    58. {
    59.     private int frequency;
    60.     private int channelConfiguration;
    61.     private volatile boolean isPaused;
    62.     private File fileName;
    63.     private volatile boolean isRecording;
    64.     private final Object mutex = new Object();
    65.     // Changing the sample resolution changes sample type. byte vs. short.
    66.     private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    67.  
    68.     /**
    69.      *
    70.      */
    71.     public Recorder() {
    72.         super();
    73.         this.setFrequency(11025);
    74.         this.setChannelConfiguration(AudioFormat.CHANNEL_CONFIGURATION_MONO);
    75.         this.setPaused(false);
    76.     }
    77.  
    78.     public void run()
    79.     {
    80.         // Wait until we
     

    Advertisement

Share This Page

Loading...