如何使用命令行在Android模拟器上启用媒体选项

时间:2012-04-07 07:24:08

标签: android android-emulator

在我的应用程序中我正在录制语音,因此我需要设置我的模拟器能够录制语音。 我在谷歌搜索了我得到了一些解决方案,需要通过手动媒体选项启动模拟器。我使用以下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获取有效选项列表

1 个答案:

答案 0 :(得分: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();
}

}

希望这对你有所帮助。让我们知道它是怎么回事,或者如果你需要进一步的帮助。