Immediate Audio Input & Output Android

前端 未结 4 1206
感情败类
感情败类 2021-02-18 16:43

In my Android App, I would like to take in some audio from the mic of the smartphone and play it immediately, live, like a microphone, with no lag. I am currently thinking of us

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-18 17:38

    As far as I can think it can be done in a very simple way. I haven't tried it,but you try it. I think it'll work:

    Create two threads one for recording another for playing. Say the threads are TRecord and TPlay.

    In TRecord's run method do this :

    public void run(){
            MediaRecorder mRecorder = null;
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                //todo
            }
    
            mRecorder.start();
    }
    

    And it TPlay's run method do this :

    public void run() {
    MediaPlayer   mPlayer = null;
    mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(mFileName);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                //todo
            }
    }
    

    Now on mainactivity simply create two threads. First start the TRecord thread then Tplay . Try it.

    here is the code for file extension:

    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
     mFileName += "/audiorecordtest.3gp";
    

提交回复
热议问题