【问题标题】:android voice recorder application安卓录音机应用
【发布时间】:2011-08-23 20:55:45
【问题描述】:

我想在 android 3.0 中创建一个录音机应用程序。场景是当我点击record 按钮时,它必须开始录制,当我点击stop 按钮时,它必须停止录制。

录制语音后,我想在单击play 按钮时播放录制的声音。当我点击stop playing 按钮时,它必须停止播放。我曾尝试使用代码来录制语音。但它以3gpp 格式存储文件。所以它不在ma设备中播放。所以我尝试将输出格式更改为“AMR_NB”。但是当我单击停止按钮时它崩溃了。任何人都可以提供代码,否则请帮助我通过编辑我的代码来使其工作。 这是我的助手类...

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

  /**
   * Creates a new audio recording at the given path (relative to root of SD card).
   */
  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;
  }

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

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    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();
  }
}

这是我的活动课

public class audiorecording extends Activity {
private Button start;
private Button stop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    start=(Button)findViewById(R.id.start);
    stop=(Button)findViewById(R.id.stop);
    final AudioRecorder recorder = new AudioRecorder("/audiometer/temp");



    start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
             try {
                    recorder.start();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


        }
    })  ;       

    //….wait a while

        stop.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                try {
                    recorder.stop();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        })  ;

    }
}

【问题讨论】:

    标签: android recording voice-recording


    【解决方案1】:

    输入android.permission.RECORD_AUDIO

    android.permission.WRITE_EXTERNAL_STORAGE
    

    在清单中?...

    【讨论】:

      【解决方案2】:

      你可以使用setOutputFormat (int output_format)

      设置录制过程中生成的输出文件的格式。在 setAudioSource()/setVideoSource() 之后但在 prepare() 之前调用它。 使用 H.263 视频编码器和 AMR 音频编码器时,建议始终使用 3GP 格式。使用 MPEG-4 容器格式可能会使某些桌面播放器感到困惑。


      Audalyzer Android 音频分析器

      这是一款适用于 Android 的简单音频分析器。它将来自麦克风的声音读数显示为波形显示、频谱和 dB 表。 dB 级别与设备的最大输入级别相关。


      Android MediaRecorder

      使用MediaRecorder录制音频的一个常见案例如下:

      MediaRecorder recorder = new MediaRecorder();
       recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
       recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
       recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
       recorder.setOutputFile(PATH_NAME);
       recorder.prepare();
       recorder.start();   // Recording is now started
       ...
       recorder.stop();
       recorder.reset();   // You can reuse the object by going back to setAudioSource() step
       recorder.release(); // Now the object cannot be reused
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多