title: Android MediaPlayer reset freezes UI
body: <p>I have a problem with the Android <code>MediaPlayer</code> when changing the <code>dataSource</code> of the player. According the specification of the <code>MediaPlayer</code> (<a href="http://developer.android.com/reference/android/media/MediaPlayer.html" rel="noreferrer">http://developer.android.com/reference/android/media/MediaPlayer.html</a>) I have to <code>reset</code> the player when changing the <code>dataSource</code>. This works fine, but as soon as the <code>channelChanged</code> method is called twice in quick succession the <code>MediaPlayer.reset</code> freezes the UI. I profile the code as seen here:</p>
  
  <pre><code>public void channelChanged(String streamingUrl)
  {
      long m1 = System.currentTimeMillis();
      mMediaPlayer.reset();
      long m2 = System.currentTimeMillis();
      try
      {
          mMediaPlayer.setDataSource(streamingUrl);
      }
      catch (IOException e)
      {
          e.printStackTrace();
      }
      long m3 = System.currentTimeMillis();
      mMediaPlayer.prepareAsync();
      long m4 = System.currentTimeMillis();
      Log.d("MEDIAPLAYER", "reset: " + (m2 - m1));
      Log.d("MEDIAPLAYER", "setDataSource: " + (m3 - m2));
      Log.d("MEDIAPLAYER", "preparing: " + (m4 - m3));
  }
  </code></pre>
  
  <blockquote>
    <p>reset: 3</p>
    
    <p>setDataSource: 1</p>
    
    <p>preparing: 0</p>
    
    <p>reset: 3119</p>
    
    <p>setDataSource: 2</p>
    
    <p>preparing: 1</p>
  </blockquote>
  
  <p>So apparently the <code>reset</code> is blocked by the <code>asynchronous preparing</code> of the first call (when I wait until the first stream starts and then call <code>channelChanged()</code> again, everything is fine).</p>
  
  <p>Any ideas how to solve the problems? Should I execute the whole method in a separate thread? Basically I want to avoid that, because it seems not to be a good coding style and can possibly cause some further issues, e.g. when the user tries to start the player again, but the player is still in the <code>reset</code> method, which on the other hand seems to wait for the <code>asyncPrepare</code> method. It is not clear how the player would behave... </p>
  
  <p>Is there any other good solution? </p>