我正在尝试将录制的音频文件保存在我希望它成为默认文件夹的文件夹中.但是我不知何故没有这样做.
我的代码:
Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/sound_"+ String.valueOf(System.currentTimeMillis()) + ".amr"));
recordIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(recordIntent, RESULT_OK);
它确实调用了录音机应用.而且当我按下停止按钮时,它返回到我的应用程序,并有一个祝酒词说它已保存.但是,而不是保存在“记录”文件夹中,而是保存在默认文件夹中.
我意识到logcat中有错误msg:
01-29 01:34:23.900: E/ActivityThread(10824): Activity com.sec.android.app.voicerecorder.VoiceRecorderMainActivity has leaked ServiceConnection com.sec.android.app.voicerecorder.util.VRUtil$ServiceBinder@405ce7c8 that was originally bound here
我不确定在调用相机应用程序时代码出了什么问题.
解决方法:
通过这种方式,用MediaRecorder录制:
要开始录制:
public void startRecording()
{
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try
{
recorder.prepare();
recorder.start();
}
catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
停止:
private void stopRecording()
{
if(null != recorder)
{
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
对于选定的文件夹:
private String getFilename()
{
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
标签:android,audio-recording,voice-recording
来源: https://codeday.me/bug/20191011/1889549.html