Question

I use the following code to record data from the microphone and then play it back. I've learned that the buffer sizes must match. The problem is that after the record, nothing is played. Instead I got messages on the log like this:

10-07 00:12:09.187: WARN/AudioTrack(3719): obtainBuffer() track 0x1df1a8 disabled, restarting 10-07 00:12:10.351: WARN/AudioTrack(3719): obtainBuffer() track 0x1df1a8 disabled, restarting

Here is the code. What do I do wrong?

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.w("DEBUG", "Audio record");

    int frequency = 44100;
    int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    AudioRecord audioRecord = null;


    final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);

    audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);


    Log.w("DEBUG", "Buffer size: " + bufferSize);
    //capture data and record to file
    int readBytes = 0, writtenBytes = 0;


    try
    {

      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
              frequency, channelConfiguration,
              audioEncoding, bufferSize);
      // audioRecord = findAudioRecord();

      short[] buffer = new short[bufferSize];
      byte[] data = new byte[bufferSize];
      if (audioRecord == null || (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED))
      {
        Log.w("DEBUG", "Can't start");
        //Log.w("DEBUG", "status: " + audioRecord.getState());
        return;
      }

      audioRecord.startRecording();


      int sampleNumber = 0;


      // while (sampleNumber < 30)
      {
        readBytes = audioRecord.read(buffer, 0, bufferSize);
        if (AudioRecord.ERROR_INVALID_OPERATION != readBytes)
        {
          Log.w("DEBUG", "Writing");
          writtenBytes += audioPlayer.write(data, 0, readBytes);
        }
        Log.w("DEBUG", "Sample number" + sampleNumber);


        sampleNumber++;
      }
      if (audioPlayer.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
      {
        Log.w("DEBUG", "Playing");
        audioPlayer.play();
      }

    } catch (IllegalArgumentException e)
    {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalStateException e)
    {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } finally
    {
      if (audioRecord != null && audioRecord.getState() == AudioRecord.STATE_INITIALIZED)
      {
        audioRecord.stop();
        audioRecord.release();
      }

    }
  }
Was it helpful?

Solution

The phone's volume wasn't high enough and I didn't talk close enough to the microphone :) The echo is still an issue, but at least I can hear the sounds at last.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top