我想在Android3.0中创建一个录音器应用程序。场景是,当我点击record
按钮时,它必须开始录制,而当我点击stop
按钮时,它必须停止录制。
录制语音后,我想在单击play
按钮时播放录制的声音。当我点击stop playing
按钮时,它必须停止播放。我试过用一个代码来录音。但是它以3gpp
格式存储文件。所以它不能在设备上播放。所以我试着把输出格式改成‘AMR_NB’,但是当我点击停止按钮的时候它就崩溃了。谁可以提供一个代码,否则请帮助我通过编辑我的代码它的工作。这是我的助手类..。
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.AMR_NB);
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();
}
}
这是我的活动课
public class audiorecording extends Activity {
private Button start;
private Button stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
final AudioRecorder recorder = new AudioRecorder("/audiometer/temp");
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
recorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}) ;
//….wait a while
stop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
recorder.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}) ;
}
}
发布于 2011-05-12 06:42:03
您可以使用setOutputFormat (int output_format)
设置录制过程中生成的输出文件的格式。在setAudioSource()/setVideoSource()之后但在prepare()之前调用此函数。在使用H.263视频编码器和AMR音频编码器时,建议始终使用3GP格式。使用MPEG-4容器格式可能会使一些桌面播放器感到困惑。
安卓音频分析器Audalyzer
这是一个简单的安卓音频分析器。它将麦克风的声音读数显示为波形显示、频谱显示和dB计量器。dB级别与设备的最大输入级别相关。
使用MediaRecorder录制音频的常见情况如下:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
发布于 2012-03-31 10:57:42
Put android.permission.RECORD_AUDIO
android.permission.WRITE_EXTERNAL_STORAGE
在清单中?.。
https://stackoverflow.com/questions/5974223
复制相似问题