Java.lang.IllegalStateException: Cannot get AudioTrack pointer for write ()

I am getting the following error: java.lang.IllegalStateException: Cannot get AudioTrack pointer for write () and I am trying to do this:

// Get the minimum buffer size required for the successful creation of an AudioRecord object.
    bufferSizeInBytes = AudioRecord.getMinBufferSize(
      hz,
      AudioFormat.CHANNEL_IN_MONO,
      AudioFormat.ENCODING_PCM_16BIT
    );

    bufferSizeInShorts = (bufferSizeInBytes /2);

    // Initialize Audio Recorder. 
    arec = new AudioRecord( 
      MediaRecorder.AudioSource.VOICE_RECOGNITION, 
      hz, 
      AudioFormat.CHANNEL_IN_MONO,
      AudioFormat.ENCODING_PCM_16BIT, 
      bufferSizeInBytes 
    );

    if (AcousticEchoCanceler.isAvailable()) {
            AcousticEchoCanceler echoCanceller = AcousticEchoCanceler.create(arec.getAudioSessionId());
            if (echoCanceller != null) {
        //        echoCanceller.setEnableStatusListener(this);
                echoCanceller.setEnabled(true);
            }
        } else { Log.e("configAudio", "Echo Canceler not available");}

    if (NoiseSuppressor.isAvailable()) {
            NoiseSuppressor noiseSuppressor = NoiseSuppressor.create(arec.getAudioSessionId());
            if (noiseSuppressor != null) {
            //    noiseSuppressor.setEnableStatusListener(this);
                noiseSuppressor.setEnabled(true);
            }
        } else { Log.e("configAudio", "Noise Suppressor not available");
          aManager.setParameters("noise_suppression=auto");
        }

    //Initialize AudioTrack               
        atrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                        hz,
                        AudioFormat.CHANNEL_OUT_MONO,
                        AudioFormat.ENCODING_PCM_16BIT,
                        bufferSizeInBytes,
                        AudioTrack.MODE_STREAM);

        atrack.setPlaybackRate(pbhz);

    // Start Recording & Playing 
        audioBuffer = new short[bufferSizeInShorts]; 
        arec.startRecording(); 
        atrack.play();
        onRec = true;

     try{   
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub\
                while (onRec) {
                          shortsRead = arec.read(audioBuffer, 0, bufferSizeInShorts);
                      }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub\
                while (onRec) { 
                   atrack.write(audioBuffer, 0, audioBuffer.length);
                 } 
                }

        }).start();

      

Any hint what might be causing this error?

+3


source to share





All Articles