title: Garbage Collection causes : MediaPlayer finalized without being released
body: <p>After a lot of debugging I finally found what is causing this error! Garbage Collection!</p>
  
  <p>I have a video playing in media view and in the background I am looking for new videos from a Rest API.</p>
  
  <p>Every now and then I see the Garbage collection running:</p>
  
  <pre><code>02-22 13:14:57.969: D/dalvikvm(16888): GC_EXPLICIT freed 152K, 4% free 6746K/6979K, paused 2ms+2ms
  </code></pre>
  
  <p>And the straight after that:</p>
  
  <pre><code>02-22 13:14:57.969: W/MediaPlayer-JNI(16888): MediaPlayer finalized without being released
  </code></pre>
  
  <p>So I tested it by calling System.gc() every 5 seconds.</p>
  
  <p>As soon as the first GC is called it happens!</p>
  
  <pre><code>02-22 13:19:47.813: D/dalvikvm(17060): GC_EXPLICIT freed 167K, 5% free 6745K/7047K, paused 2ms+2ms ---- I call GC
  02-22 13:19:47.813: W/MediaPlayer-JNI(17060): MediaPlayer finalized without being released ---- VIDEO PLAY INTERRUPTED
  </code></pre>
  
  <p>Why does this happen? Can I prevent it?</p>
  
  <p>Playing the video:</p>
  
  <pre><code>private void playMedia(int playListIndex) throws IOException {
          File mediadir = getDir("tvr", Context.MODE_PRIVATE);
          filelist = mediadir.listFiles();
          Log.i("media player", "play media!");
          String path = filelist[playListIndex].getAbsolutePath();
          FileInputStream fileInputStream = new FileInputStream(path);
          final Uri uri = Uri.parse(path);
          String filename = filelist[playListIndex].getName();
          if (filename.contains("image")) {
              imageView = (ImageView)findViewById(R.id.imageView);
              imageView.setVisibility(View.VISIBLE);
              imageView.setImageURI(uri);
              mHandler.postDelayed(new Runnable() {
                  public void run() {
                      imageView.setVisibility(View.GONE);
                      imageView.setImageURI(uri);
                      onCompletion(null);
                  }
              }, 4000);
          } else if (filename.contains("video")) {
  
              MediaPlayer pl = new MediaPlayer();
              pl.setOnCompletionListener(this);
              pl.setDisplay(holder);
              pl.setDataSource(fileInputStream.getFD());
              pl.prepare();
              pl.start();
          }
      }
  </code></pre>
  
  <p>And when it is done:</p>
  
  <pre><code>@Override
      public void onCompletion(MediaPlayer mp) {
          Log.i("media player", "play next please!");
          if (mp != null) {
              mp.release();
          }
  //      play next video
          currentMedia++;
          if (currentMedia &gt; playList.size() - 1) {
              currentMedia = 0;
          }
          try {
              playMedia(currentMedia);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  </code></pre>