Mohsen Bahman
Mohsen Bahman

Reputation: 1092

How to show a progress dialog at loading audio on MediaPlayer obj

I want to load an audio file from internet on MediaPlayer object.

it's my code:

try {
    mp.setDataSource(AUDIO URL);
    mp.prepare();
    mp.start();
}
catch (IOException e) { e.printStackTrace(); }
catch (IllegalArgumentException e) { e.printStackTrace(); }
catch (IllegalStateException e) { e.printStackTrace(); }

but problem is that it's take a lot of time loading audio.

Help me to show a spinning progress dialog before loading audio to after that.

If question is incomplete tell me to complete decreasing.

Upvotes: 1

Views: 6169

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

you will need to add MediaPlayer.OnPreparedListener to MediaPlayer to show progress dialog when it's preparing as :

ProgressDialog progressDialog = ProgressDialog.show(this, 
                            "Loading Title", "Loading Message");
mp.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
               if (progressDialog != null && progressDialog.isShowing()){
                  progressDialog.dismiss();
               }
                mp.start();
         }
     });

Upvotes: 3

Gjordis
Gjordis

Reputation: 2550

Not quite a dublicate, but this question has a progress bar while preparing, it would be "quite" easy to modify to progress dialog or something else:

Progress Bar while media player is preparing

Upvotes: 1

Related Questions