Pages

Friday 26 July 2013

Recording audio in android



To access the recording by using intent We use Intent.ACTION_GET_CONTENT in Intent function.
Then, We have to set type to intent.setType("audio/*").
       
Function

Intent(Intent.ACTION_GET_CONTENT).
setType("audio/*").
startActivityForResult

     Coding Part:

           XML:

       <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        android:layout_centerHorizontal="true"
        android:textStyle="bold"
        android:text="Click" />

Activity:

        public class Recording extends Activity {

           Button clickBtn;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.menu);
        clickBtn=(Button)findViewById(R.id.button1);

clickBtn.setOnClickListener(new OnClickListener() {
 
      @Override
      public void onClick(View v) {
   // TODO Auto-generated method stub
      public void onClick(View arg0) {
 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("audio/*");
    startActivityForResult(intent, 0);
              }
});
}
 }

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
                 super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

                 switch(requestCode) {
                 case 0:
                     if(resultCode == RESULT_OK){
                     Uri selectedImage = imageReturnedIntent.getData();
                             String[] filePathColumn = {MediaStore.Images.Media.DATA};

                                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                                cursor.moveToFirst();

                                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                               //file path of captured image
                                filePath = cursor.getString(columnIndex);
                                //file path of captured image
                                File f = new File(filePath);
                                filename= f.getName();
                             
                                Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show();
                                Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show();
                                cursor.close();
                               
                              //Convert file path into bitmap image using below line.
                               // yourSelectedImage = BitmapFactory.decodeFile(filePath);
                             Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show();
                           
                               //put bitmapimage in your imageview
                               //yourimgView.setImageBitmap(yourSelectedImage);  


}
                     break;
                     }}}

Add these things in your android.manifest:


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

No comments:

Post a Comment