title: How to Resolve a AudioFlinger server died! Error in Android?
body: <p>New to Android, this is my first app and I've been at this issue for a few days now. My app lets users play 6 background instrumentals during game play. The music is all .mp3 format and <strong>housed directly</strong> in the app in the R.raw file folder - <strong>NOTHING is being streamed from a server</strong>.</p>
  <p>The issue is when a track is selected the track plays for a second, freezes for a second or two, and the picks up and starts to play on. This always happens the first time a track is selected and then usually on the 2nd and then if I keep testing by playing different tracks the error becomes 50/50 as to whether it will happen or not.</p>
  <p>I've always insured that the mediaPlayer == null prior to requesting it play a track. I've also thought it might be a memory issue, so prior to the mediaPlayer call I check the memory with this code:</p>
  <pre><code>private ActivityManager.MemoryInfo getAvailableMemory() {
          ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
          ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
          activityManager.getMemoryInfo(memoryInfo);
          return memoryInfo;
  }
  </code></pre>
  <pre><code> if (!memoryInfo.lowMemory) {
        Log.d(&quot;Tag&quot;, &quot;NOT LOW MEMORY&quot;);
   } else {
        Log.d(&quot;Tag&quot;, &quot;LOW MEMORY!!! LOW MEMORY!!!!&quot;);
   }
  </code></pre>
  <p>Each time the result is &quot;NOT LOW MEMORY.&quot; I've also check for memory leaks and haven't found any in my app. My last resort was to play the track on background thread but I got the same result. What I see in the Logcat(Info) are these errors:</p>
  <pre><code>E/AudioSystem-JNI: Command failed for android_media_AudioSystem_error_callback: -32
  W/AudioSystem: AudioFlinger server died!
  W/AudioSystem: AudioPolicyService server died!
  </code></pre>
  <p>My latest test was to super simplify the code, I stripped it down as best I could. When the player clicks the button to play the track, this is the direct code:</p>
  <pre><code>boolean stopThread = false;
  startNewBackgroundThreadForMediaPlayer(stopThread);
  </code></pre>
  <p>The method:</p>
  <pre><code>private void startNewBackgroundThreadForMediaPlayer(boolean stopThread) {
          BackgroundMediaPlayerThreadRunnable runnable = new BackgroundMediaPlayerThreadRunnable(stopThread);
          new Thread(runnable).start();
      }
  </code></pre>
  <p>The inner class:</p>
  <pre><code>private class BackgroundMediaPlayerThreadRunnable implements Runnable {
  
          boolean stopThread;
  
          BackgroundMediaPlayerThreadRunnable(boolean stopThread) {
              this.stopThread = stopThread;
          }
  
          @Override
          public void run(){
  
              if(!stopThread) {
                  MediaPlayer player = new MediaPlayer();
                  player = MediaPlayer.create(SongsActivity.this, R.raw.song1);
                  player.start();
              } 
  }
  </code></pre>
  <p>I've left some code out, like about stopping the song/thread. I had more complicated code checking for the media player's status(==null?), saving song to the Shared Preferences, etc. Whether this code or the other, the results have been the same.</p>
  <p>I've exhausted all my resources, I haven't been able to find much regarding an error like this. I know it is not the actual mp3s because they play OK sometimes. I'm testing on a real device, a Pixel 4XL. Also, this error just started happening, this same code work perfectly fine a 3-4 days ago. Can anyone assist me? Is there an alternative to mediaPlayer that I should try? This is my LAST big obstacle for my app. Thanks.</p>