title: Android E/IAudioManager: trackRecorder() failed, riid is -1
body: <p>I want to record ambient sound 24/7 to detect noise on Android.</p>
  <p>So, I implemented the following code in the foreground service.
  In returnData(), it collects noise data per 1 sec.</p>
  <pre><code>private void returnData(){
      i(&quot;returnData&quot;);
      new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
              start();
  
              noise = changedNoise;
  
              stop();
              // Call the Runnable again to execute repeatedly
              new Handler().postDelayed(this, 1000);
          }
      }, 1000); // delaying for 1 second
  }
  
  private double changedNoise;
  
  private static Timer timer;
  
  /*************************************************
   * Noise Detection
   *************************************************/
  
  private void start() {
      i(&quot;start()&quot;);
      prepare();
      timer.schedule(new RecordingTask(), 0, 1000);
  
  }
  
  private void prepare() {
      i(&quot;setRecordingTimerTask&quot;);
      timer = new Timer();
  }
  
  private class RecordingTask extends TimerTask {
  
      private static final double REFERENCE = 0.00002;
  
      private int bufferSize;
      private AudioRecord audioRecord;
      private short[] data;
  
      @Override
      public void run() {
          Log.v(&quot;MYTAG&quot;, &quot;recording start...&quot;);
  
          initAudioRecord();
          if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
              audioRecord.startRecording();
          }
          audioRecord.read(data, 0, bufferSize);
          audioRecord.stop();
          double average = 0.0;
          for (short s : data) {
              if (0 &lt; s) average += s;
              else bufferSize--;
          }
          double x = average / bufferSize;
          audioRecord.release();
          double dB = 0.0;
          if (x != 0) {
              double pressure = x / 51805.5336;
              dB = 20 * Math.log10(pressure / REFERENCE);
              changedNoise = dB;
              if (Double.isInfinite(dB) || Double.isNaN(dB)) {
                  i(&quot;Infinity or NaN&quot;);
              }
          }
      }
  
      private void initAudioRecord() {
          bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
          bufferSize *= 4;
          if (ActivityCompat.checkSelfPermission(TriggeringService.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
              audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                      44100, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
          } else i(&quot;RECORD_AUDIO permission was denied&quot;);
          data = new short[bufferSize];
      }
  
  }
  
  public void stop() {
      i(&quot;stop()&quot;);
      if(timer != null) timer.cancel();
  }
  </code></pre>
  <p>However, after about 13 hours after the app is launched, the app always ends with an error message as below.</p>
  <p><em>E/IAudioManager: trackRecorder() failed, riid is -1</em></p>
  <p>Is there anyone who knows IAudioManager error?:(
  I could not find any information about this error.</p>