使用Android模拟器录制声音

时间:2012-03-23 16:44:59

标签: android emulation record mediarecorder

我正在尝试通过创建一个Android应用程序来录制声音。 这是代码:

这是助手类

package com.recorder;

import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;

public class AudioRecorder {

final MediaRecorder recorder = new MediaRecorder();
final String path;

/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(String path) {

    this.path = sanitizePath(path);
  }

  private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (!path.contains(".")) {
      path += ".3gp";
    }

    return Environment.getDataDirectory().getAbsolutePath() + path;
  }

  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
}

/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
 }

这是我的活动:

package com.recorder;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class RecorderActivity extends Activity {
    MediaPlayer mp=new MediaPlayer();
    ImageButton mic;
    private AudioRecorder recorder;

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.main);

     recorder = new AudioRecorder("/Mysounds1");//here is the path
    //to sdcard where we will save the recording
    }
    // Action listener for a button which record sound   
         public void mic(View view){
        Toast.makeText(this,"Recording started...",Toast.LENGTH_LONG).show();
        try {
        recorder.start();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }

当我运行代码时,我得到以下异常:

java.io.IOException:未安装SD卡。它已删除。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您还需要模拟SD卡。转到AVD - >编辑 - >在SD卡插槽中,给任何数字模拟sdcard的内存量。完成