Can not use MediaRecorder on Android Emulator. Is

2019-07-25 12:59发布

问题:

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();
  }
}

回答1:

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.