Android:暂停录音机和恢复

时间:2011-08-31 12:20:27

标签: android media recorder

我使用以下代码作为创建记录器的基础。我可以开始和停止录音,并在该位置正确保存。但现在我有一个要求暂停录音机的要求

如何暂停录音机?并恢复录音?我在三星Galaxy Ace中看到了录音应用程序,它有一个暂停按钮。

有人可以启发我。

public class audio {
     final MediaRecorder recorder = new MediaRecorder();
      final String path;

      /**
       * Creates a new audio recording at the given path (relative to root of SD card).
       */
      public audio(String path) {

        this.path = sanitizePath(path);
      }

      private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
          path = "/" + path;
        }
        if (!path.contains(".")) {
          path += ".3gpp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
      }

      /**
       * Starts a new recording.
       */
      public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // 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.");
        }

       try {
           recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(path);

            recorder.prepare();
            recorder.start();
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }
      }

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

      public void pause() {

      }

    }

1 个答案:

答案 0 :(得分:1)

查看这2页

http://developer.android.com/guide/topics/media/index.html http://developer.android.com/reference/android/media/MediaRecorder.html

根据第一篇文章有​​一个暂停()

但我没有在第二个链接上看到pause()方法,所以我不确定

第一篇文章引用的另一件事: “但是,当您调用stop()时,请注意在再次准备MediaPlayer之前不能再次调用start()。 在编写与MediaPlayer对象交互的代码时,请始终牢记状态图,因为从错误的状态调用其方法是导致错误的常见原因。“

所以也许你可以停下来()准备媒体播放器然后再次开始()