Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking audio recorder and getting the resulting file

Tags:

android

I am trying to invoke the audio recorder on Android 2.2.1 (device Samsung Galaxy POP) using the following code:

private static final int ACTIVITY_RECORD_SOUND = 1;
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, ACTIVITY_RECORD_SOUND);

This invokes the recorder successfully. In my activity result i do the following:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
                         case ACTIVITY_RECORD_SOUND:
                             data.getDataString();
                         break;
                            }
                }
       }

After i complete the recording i press back on the audio recorder which returns the control to the onActivityResult method as expected, but my resultCode is always 0 (which is Activity.RESULT_CANCELED) and my data is null. Am i missing out on something here? Kindly help me with this. This works on the emulator but not on the device. Thanks in advance.

like image 274
S.A.Norton Stanley Avatar asked Nov 26 '22 17:11

S.A.Norton Stanley


1 Answers

This works for me:

   @Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
      switch(requestCode) {
         case Flashum.TAKE_MUSIC:
         case Flashum.TAKE_VOICE:
            if (resultCode == Activity.RESULT_OK)
            {
               Log.i(Flashum.LOG_TAG, "onActivityResult got new music");
               Bundle extras = data.getExtras();
               try {
                  Uri u = data.getData();
                  String imageUri;
                  try {
                     imageUri = getRealPathFromURI(u);
                  } catch (Exception ex) {
                     imageUri = u.getPath();
                  }
                  File file = new File(imageUri);
                  FragmentFlash fragmentFlash = (FragmentFlash)mTabsAdapter.getFragment("flash");
                  if (fragmentFlash != null)
                     fragmentFlash.gotMusic(file.getPath());
               } catch (Exception ex) {
                  String s = ex.toString();
                  Log.i(Flashum.LOG_TAG, "onActivityResult " + s);
               }
            }
            else
            {
               Log.i(Flashum.LOG_TAG, "onActivityResult Failed to get music");
            }
            break;
      }
   }

   public String getRealPathFromURI(Uri contentUri) {
      String[] proj = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
   }
like image 85
charles young Avatar answered Nov 29 '22 05:11

charles young