如何以编程方式将录制的音频文件保存在另一个文件夹中?

时间:2012-01-28 08:27:15

标签: android audio-recording voice-recording

我正在尝试将录制的音频文件保存在我想要的文件夹中,而不是默认文件夹。但不知怎的,我没有这样做。

我的代码:

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中有错误信息:

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

当我调用相机应用程序时,我不确定代码是否有效。

3 个答案:

答案 0 :(得分:10)

以这种方式,用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");
        }

答案 1 :(得分:0)

我已经找到了解决这个问题的方法,即使它需要一轮才能完成,而不是直截了当,但它是我得到的最好的,它也有效。

而不是使用额外包含的语音记录器应用程序,我只是在没有任何输入的情况下调用它:

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(recordIntent, 1111);

然后,添加一个onActivityResult,请求代码== 1111(取决于您放置的内容)并从记录器“Sounds”的默认文件夹中检索包含扩展名“3ga”的最后修改文件

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1111)
    {
         File folder = new File(Environment.getExternalStorageDirectory(), "/Sounds");
         long folderModi = folder.lastModified();

    FilenameFilter filter = new FilenameFilter() 
    {
        public boolean accept(File dir, String name) 
        {
            return (name.endsWith(3ga));
        }
    };

    File[] folderList = folder.listFiles(filter);

    String recentName = "";

    for(int i=0; i<folderList.length;i++)
    {
        long fileModi = folderList[i].lastModified();

        if(folderModi == fileModi)
        {
            recentName = folderList[i].getName();
        }
    }
}
这样,我可以得到文件的名称,并用它进行修改(例如重命名)。

希望这有助于其他人。 =)

答案 2 :(得分:0)

之前我用过这种方式,对我来说没问题!

private MediaRecorder mRecorder = null;
    public void startRecording() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile(getFilename());
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mRecorder.start();  
        }
    }

停止录制:

        public void stopRecording() {

        if (mRecorder != null) {
            mRecorder.stop();
            timer.cancel();
            mRecorder.release();
            mRecorder = null;
    }
    }

保存文件:

        @SuppressLint("SdCardPath")
    private String getFilename() {
         file = new File("/sdcard", "MyFile");

        if (!file.exists()) {
            file.mkdirs();
        }

        return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
    }

if you want to delete folder after recording use this in function of stopping:

    boolean deleted = file.delete();

I hope it can be helpful.