【问题标题】:how to save the recorded audio files in another folder programmatically?如何以编程方式将录制的音频文件保存在另一个文件夹中?
【发布时间】:2012-02-21 01:40:05
【问题描述】:

我正在尝试将录制的音频文件保存在我希望它不是默认文件夹的文件夹中。但不知何故我没有这样做。

我的代码:

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

当我调用相机应用程序时,我不确定代码是否正常工作出了什么问题。

【问题讨论】:

  • 对我来说也是这样,视频和图像捕获工作正常,音频不行,您找到解决方案了吗?
  • @TamimAttafi 嘿,这是一个很老的问题,所以现在可能无法提供帮助,但我之前已经在下面给出了我的解决方案。希望对您有所帮助。
  • 你好,我最终用自定义布局创建了自己的录音机。谢谢!

标签: android audio-recording voice-recording


【解决方案1】:

我以前用过这种方式,对我来说还可以!

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.

【讨论】:

    【解决方案2】:

    我找到了解决这个问题的方法,尽管它需要一轮而不是直截了当,但它是我所拥有的最好的并且它也有效。

    我没有调用包含额外内容的录音机应用程序,而是直接调用它而无需任何输入:

    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();
            }
        }
    }
    

    这样,我可以得到文件的名称,也可以用它进行修改(例如重命名)。

    希望这对其他人有所帮助。 =)

    【讨论】:

      【解决方案3】:

      这样做,用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");
              }
      

      【讨论】:

      • erm...谢谢,但我宁愿调用默认应用程序,因为它具有我需要的所有 gui 和方法。 =)
      • 只需添加&lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多