【问题标题】:invoke audio recorder with intent and save voice in sdcard有意调用录音机并将语音保存在SD卡中
【发布时间】:2015-09-07 14:36:19
【问题描述】:

我尝试使用意图调用默认录音机并将路径发送到录音机,而不是将语音保存在我的路径中的 sdcard 中。我使用此代码:

File audioFile = new File(Environment.getExternalStorageDirectory().getPath() + "/nabege" + File.separator + "audio"
            + File.separator + System.currentTimeMillis() + ".amr");
    Uri audioUri = Uri.fromFile(audioFile);
    record_audio_Intent.putExtra(MediaStore.EXTRA_OUTPUT, audioUri);
    startActivityForResult(record_audio_Intent, 0);

此应用将语音保存在默认路径中。此路径(nabege/audio)在 sdcard 中创建并存在。请帮助

【问题讨论】:

  • 你有什么问题?
  • 我希望录制的语音保存在 sdcard/nabege/audio 但它保存在默认路径中。
  • 试试 Environment.getExternalStorageDirectory().getAbsolutePath()
  • 我像这样更改我的代码:File audioFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/nabege" + File.separator + "audio" + File.separator + System. currentTimeMillis()+ ".m4a");但它不起作用

标签: android audio android-intent


【解决方案1】:

通过 Intent 进行录音始终将录制的文件保存到 sdcard 的根目录(或默认路径)。此外,某些手机型号可能不支持此意图。

如果要将录制的文件保存到自定义路径,则必须直接使用 MediaRecorder。

像这样:

MediaRecorder recorder = null;

private void startRecording(File file) {
   if (recorder != null) {
      recorder.release();
   }
   recorder = new MediaRecorder();
   recorder.setAudioSource(AudioSource.MIC);
   recorder.setOutputFormat(OutputFormat.THREE_GPP);
   recorder.setAudioEncoder(AudioEncoder.AMR_WB);
   recorder.setOutputFile(file.getAbsolutePath());
   try {
      recorder.prepare();
      recorder.start();
   } catch (IOException e) {
      Log.e("giftlist", "io problems while preparing [" +
            file.getAbsolutePath() + "]: " + e.getMessage());
   }
}

【讨论】:

  • 这样可以暂停录音吗?如果你的回答是肯定的:怎么做?
  • MediaRecorder 没有暂停和恢复方法。您需要改用停止和启动方法。您可以这样做的一种方法是:您可以将原始数据记录到临时文件中,当您想要暂停时,停止记录并将输出附加到您的实际目标文件,并在重新编码会话结束时对整个原始数据进行 endode。这有点棘手 - 但这里有一个例子:github.com/lassana/continuous-audiorecorder
  • 谢谢。我试试这个样本。
  • 在这个项目(github)中使用了 com.googlecode 库。如何将此库添加到 myproject?
  • 假设您使用的是 eclipse,我可以告诉您一个捷径(如果您不想处理 Maven 构建系统)。从这里下载 jar 文件:code.google.com/p/mp4parser/downloads/…(忽略警告)。将下载的 jar 复制到 eclipse 工作区的 libs 文件夹中。
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 2016-05-24
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 2019-01-14
相关资源
最近更新 更多