Recording Audio and Playing Audio at the same time

2019-07-05 05:57发布

问题:

In my application i am recording input from the microphone and at a certain point i am playing a sound (while the recording is still going). Now the recorder "stops" to record (mrec.stop() may still be used), the LogCat entry for this seems to be:

    07-03 18:40:34.811: INFO/AudioHardwareALSA(2179): Output standby called!!. Turn off PCM device.

I am testing on two phones, one is a Samsung Galaxy S and one is a Samsung Galaxy 3. On the Galaxy S everything works as expected (which is that the recording still takes place). The difference between the two phones besides the specs is that the Galaxy S has 2.2.1 and the Galaxy 3 2.2 I figured, that a workaround would be to initialize the mediarecorder again, but this makes the application stutter, which is not wanted.

Here is more information how the mediaplayer and the mediarecorder are set up:

    void Init()
    {
      mp = MediaPlayer.create(getContext(), R.raw.pop);
      mrec = new MediaRecorder();
      mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
      mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      [..]
      mrec.setOutputFile(audiofile.getAbsolutePath());
      mrec.prepare();
      mrec.start();
    }

    void Event()
    {
      mp.start();
    }

Why does the recording "stop"? Is there a way to halt the stop? Is this the desired behaviour? How can i find out what's wrong?

Any tips are appreciated.

回答1:

Try using the native basic player and recorder ... These are accessible thorough AudioTrack class and other related apis like audio format. Using them you can record and play pcm audio data in chunks or buffer (explicit byte arrays) and use a loop to write the buffer to Anything you want. May be that would allow to do recoding and playing simultaneously

Good luck