做了一个简单的声音管理器中,您可以录制声音:
public class SoundManager {
private static final String TAG = SoundManager.class.getSimpleName();
public boolean isRecording = false;
private MediaRecorder recorder;
private String audioFileName = "sound";
private Context mContext;
private String storePath;
public SoundManager(Context context) {
this.mContext = context;
}
public void onRecord(boolean toStart) {
if (toStart) {
try {
startRecording();
isRecording = true;
} catch (IOException e) {
Log.d("Tag", e.toString());
}
} else {
stopRecording();
}
}
private void startRecording() throws IOException {
stopRecording();
audioFileName = UUID.randomUUID().toString();
storePath = new File(mContext.getExternalFilesDir(null), "/images/"+ audioFileName+".3gp").getAbsolutePath();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setOutputFile(storePath);
recorder.prepare();
recorder.start();
}
private void stopRecording() {
if (isRecording && recorder != null) {
try {
recorder.stop();
recorder.release();
} catch (RuntimeException e) {
recorder.release();
Log.d(TAG, e.toString());
}
recorder = null;
isRecording = false;
}
}
public File getAudioOutputPath() {
return new File(storePath);
}
}
用途:
- 开始记录:
soundManager.onRecord(true);
- 停止录音:
soundManager.onRecord(false);
- 得到声音文件路径:
soundManager.getAudioOutputPath();
添加音频允许在你的清单:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
`
我很感激,但我希望能够做到从缓冲区中是抢音频并将其自己写入文件,因此试图学习AudioRecord,该AudioRecord具有该回调例程。 我只是不出来,所以一直在寻找的代码在网上,我会再从学习,并从那里建立的一个例子。 –
很抱歉听到这个消息。你能告诉我你为什么在录音时需要缓冲区吗?如果你需要查看某些东西,那么我有办法做到这一点。 – Sayem
只是希望能够自己写作。无论是实时分解还是实时分析。我所有的谷歌都指向AudioRecord类。就在学习之初,它正在寻找一个在线示例。 –