1

I'm trying to record sound using Android Emulator. I know that this question is popular over the internet, I checked many posts, it seems that only one person succeded: Can the Android emulator record and play back audio using pc hardware?. (it think he used

File fTmFile; insteadof String fTmpFile;

which i also tried). And following Philip's advice and the official site tutorial http://developer.android.com/guide/topics/media/audio-capture.html and also other resources, I'm still not able to record. My application throws exception at line:

fMediaRecorder.prepare(); 

more exactley, this is what I first get:

W/System.err(1042): java.io.FileNotFoundException: /mnt/sdcard/audiorecordtest.3gp (Permission denied)

which makes me think is something wrong with the storage location, because even I added 'SD Card Support' property for the emulator with size 256 MiB, I'm not able to acces it, furthermore I can see in the emulator the message: "Your phone does not have a SD Card inserted" when I go to Music.

I added both audio record and external storage permissions, in AndroidManifest.xml and both audio (record+playback) hardware settings to the emulator 2.3.3 on Win 7. Is anything wrong within my app, the way I storage the file or something else? Please, if anybody has any idea feel free to share, it will be appreciated.

Here is the full source code:

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class RecordSoundActivity extends Activity {

  private MediaRecorder fMediaRecorder = null;
  private Button btnrecord;
  private Button btnstop;
  String fTmpFile;

  public RecordSoundActivity() {

    fTmpFile = Environment.getExternalStorageDirectory().getPath();
    fTmpFile += "/audiorecordtest.3gp";
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnrecord = (Button) findViewById(R.id.button1);
    btnstop = (Button) findViewById(R.id.button2);

    btnrecord.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(RecordSoundActivity.this, "Recording...", Toast.LENGTH_LONG).show();
        Recording();
      }
    });

    btnstop.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        fMediaRecorder.stop();
        fMediaRecorder.release();
      }
    });
  }

  public void Recording() {
    fMediaRecorder = new MediaRecorder();
    fMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    fMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    fMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    fMediaRecorder.setAudioChannels(1);
    fMediaRecorder.setAudioSamplingRate(8000);

    fMediaRecorder.setOutputFile(fTmpFile);

    try {
      fMediaRecorder.prepare();
    } catch (IOException e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    try {
      fMediaRecorder.start();
    } catch (IllegalStateException e) {
      // TODO: handle exception
      e.printStackTrace();
    }

    //fMediaRecorder.stop();
    //fMediaRecorder.release();
  }
}
Community
  • 1
  • 1
AlexAndro
  • 1,918
  • 2
  • 27
  • 53

1 Answers1

0

Try and see if it works for Android 4.0. I know I had some issues with the camera in the emulator, in lower version (Lower than 4.0) it just wouldn't recognize my laptop webcam. But when I tried it on 4.0, when the AVD was loading a popup message came and asked me if I want to connect the webcam to the AVD, and once I agreed it worked.

Another poster in SO asked this question too, about the camera, and changing the AVD version to 4.0 did help him.

Maybe its the same for audio recording too, as both are external hardware for the typical PC.

Jong
  • 9,045
  • 3
  • 34
  • 66
  • thanks, but my 4.0Android Emulator does not work, I don't know why, I have updated SDK but when I try to launch it, crash. Is necessary to update Eclipse to work with Android Emulator 4 ? – AlexAndro Nov 01 '11 at 08:46
  • Yes, you need to update ADT. Go to Eclipse, select the menu "Help" and pick "Check for updates", then update ADT. – Jong Nov 01 '11 at 12:35
  • SDK is fully updated,all the Android4.0 features are installed,no updates are available,but it still has strange behaviour.I'm still waiting a response from the people to see if is anything wrong with the storage location. Thanks. – AlexAndro Nov 01 '11 at 13:06
  • I have purchased a new Nexus S device and the application works fin on it. – AlexAndro Nov 03 '11 at 11:20
  • What do you mean? Give details of the problem. – Jong Nov 03 '11 at 19:13
  • The problem with the SDCard support was solved, it seems that my emulator does not always see the card, But as I said running the application on a real android-device,Nexus S, it works fine. – AlexAndro Nov 09 '11 at 11:27