Search code examples
androidprogress-barmedia-player

How can i show a ProgressBar when mediaPlayer is prepairing to play


This is the code in my main Activity:

private void initControls()
 {
  streamButton = (Button) findViewById(R.id.button_stream);
  streamButton.setOnClickListener(new View.OnClickListener()
  {
   public void onClick(View view)
   {
        loading = (ProgressBar) findViewById(R.id.progressBar1);
    Toast
    .makeText(
      MyActivity.this,
       "The following stream is about to start" + station,   
      Toast.LENGTH_LONG).show();
    player.playAudioFile(urlstring2);
    streamButton.setEnabled(false);
    notificate();
   }
  });
 }

And here is the code in witch i start the mediaplayer itself:

public class player extends MyMainActivity{

static MediaPlayer player;
private static final String TAG = player.class.getSimpleName();



public static void playAudioFile(String urlstring2) {


    player = new MediaPlayer();
    try {
        player.setDataSource(urlstring2);
        loading.setVisibility(View.VISIBLE);
        player.prepareAsync();
        //player.start();

    } catch (Throwable thr) {
        Log.e(TAG, "could not play audio", thr);
        loading.setVisibility(View.INVISIBLE);
    }
    player.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            player.start();
            loading.setVisibility(View.INVISIBLE);
        }
    });

}

public static void releasePlayer() {
    if (player != null) {
        // Player-Ressourcen freigeben
        player.release();
        player = null;
    }
}

}

What I want to do is to show the progressBar (the cicling one) when the user presses the "play" Button, and I need it to disapear when the mediaplayer starts to play. atm I just get the circle to show after the player has startet to play and never disapear ...

Edit: I got it to work. I did initiate the player like this: player.prepare(); insteas of player.prepareAsync(); So the progressbar was shown for a view milliseconds.


Solution

  • Came back to finally mark this Question as solved, so I copied what I edited into my question previously.

    I got it to work. I did initiate the player like this: player.prepare(); insteas of player.prepareAsync(); So the progressbar was shown for a view milliseconds.