简体   繁体   English

如何在android中录制音频?

[英]How to record audio in android?

I am doing a loopback test in android, which record's audio using AudioRecorder then encode the uncompressed pcm into speex after that i decode speex back to pcm and given to AudioPlayer for playing .Later case i will test between two devices P2P . 我在android中进行环回测试,使用AudioRecorder录制音频,然后将未压缩的pcm编码为speex,之后我将speex解码回pcm并发送给AudioPlayer进行播放。后来我将测试两个设备之间的P2P。 I am trying to record audio , similar to call recording in which save both sender and receiver voide into a single file mp4 format .Can anybody suggest me a good direction to proceed and also is it possible to achieve this through ffmpeg ported for android? 我正在尝试录制音频,类似于通话记录,其中将发送者和接收者的声音保存为单个文件mp4格式。任何人都建议我继续前进的良方向,也可以通过移植到Android的ffmpeg来实现这一点吗?

thanks, 谢谢,

i hope it will work for you.. set your path in" this.path = sanitizePath(path); in this line" 我希望它能为你工作..在“this.path = sanitizePath(path);在这一行中设置你的路径”

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

} }

MediaRecorder.AudioSource is a good start I guess? MediaRecorder.AudioSource是一个好的开始,我猜?

http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM