简体   繁体   中英

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 . 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?

thanks,

i hope it will work for you.. set your path in" this.path = sanitizePath(path); in this line"

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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