0

我正在使用媒体记录器和以下代码保存音频文件:

 public class AudioRecorder {

 final MediaRecorder recorder = new MediaRecorder();
 final String path;


  /**
   * Creates a new audio recording at the given path
   */
  public AudioRecorder(String path) {
  this.path = sanitizePath(path);
}

public static String sanitizePath(String path) {
   if (!path.startsWith("/")) {
      path = "/" + path;
   }
   if(path.endsWith("/")){
      path = path + "/";
  }
  return path;
 }

 /**
  * Starts a new recording.
  */
  public void start() throws IOException {
  // make sure the directory we plan to store the recording in exists
  File directory = new File(path).getParentFile();
  if (!directory.exists() && !directory.mkdirs()) {
  throw new IOException("Path to file could not be created.");
 }

   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
   recorder.setOutputFile(path);
   recorder.prepare();
   recorder.start();
  }

 /**
  * Stops a recording that has been previously started.
  */
 public void stop() throws IOException {
 recorder.stop();
 recorder.release();
 }
}

然后我使用以下代码调用这个类

 int timeOfRecroding = AppPrefs.getSettingsAdditionalTimeOfRecording() * 60 * 1000;
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
    File directory = cw.getDir("media", Context.MODE_PRIVATE);

    final String pathAndName = AudioRecorder.sanitizePath(directory.getAbsolutePath() + "/LocRec.3gp");
    final AudioRecorder audioRecorder = new AudioRecorder(pathAndName);

    if(Constants.isTest){
        showToast("Starting recording for [" + AppPrefs.getSettingsAdditionalTimeOfRecording() + "] minutes");
        showToast("Recording to path: [" + pathAndName + "]");

    }

然后当然使用 audioRecorder.start(); 和 audioRecorder.stop(); 做实际录音

录制完成后,我使用相同的 pathAndName 获取文件并将其作为电子邮件附件发送,使用以下代码获取文件

new File(new URI(AppPrefs.getInfoToSend(Constants.SERVICE_CODE_SEND_RECORDING, Constants.MESSAGE_TYPE_EMAIL)))

但这是抛出异常

URI is not absolute: /data/data/com.testrecoding.record/app_media/LocRec.3gp

感谢您的帮助,谢谢, Wassim

4

1 回答 1

0

听起来可能很愚蠢,但我找到了一个转机,而不是真正的转机,不知道我一开始是怎么错过的,直接从路径获取文件而不使用 URI

 new File("PathOfFIle")
于 2013-03-08T23:58:19.990 回答