zoukankan      html  css  js  c++  java
  • Android通过MediaRecorder API手机音频录制例子

    import java.io.File;
    import java.io.IOException;

    import android.media.MediaRecorder;
    import android.os.Environment;

    public class AudioRecorder
    {

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

    public AudioRecorder(String path)
    {
    this.path = sanitizePath(path);
    }

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

    /**
    * 开始记录
    */
    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 + “.”);
    }

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

    public void stop() throws IOException {
    recorder.stop();
    recorder.release();
    }
    }

  • 相关阅读:
    Java实现2048小游戏
    归并排序【代码】
    插入排序【代码】
    选择排序【代码】
    考试系统
    九九乘法表
    万年历
    猜数游戏
    C++知识点(杂)
    Core Data ,inverse
  • 原文地址:https://www.cnblogs.com/wanqieddy/p/2547390.html
Copyright © 2011-2022 走看看