5

在我的应用程序中,我正在录制语音,因此我需要设置能够录制语音的模拟器。我在谷歌中搜索了一些解决方案,需要通过媒体选项手动启动模拟器。我使用以下 cmd 但出现错误。

emulator -avd Test -audio-in MIC

我在 Windows 7 上使用 Android 2.2(Api 2.2)。如何在我的模拟器上启用 MIC 选项。请帮我。

我收到以下错误:

>emulator -avd Test -audio-in MIC

>unknown option: -audio-in

请使用 -help 获取有效选项列表

4

1 回答 1

9

尝试使用此示例:

package com.benmccann.android.hello;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;

/**
 * @author <a href="http://www.benmccann.com">Ben McCann</a>
*/

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.getExternalStorageDirectory().getAbsolutePath() + path;
}

/**
 * Starts a new recording.
*/
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();
}

}

希望这对您有所帮助。让我们知道进展如何,或者如果您需要进一步的帮助。

于 2012-04-07T08:00:42.797 回答