简体   繁体   中英

Unable to show ProgressDialog on mp3 media player while prepare

I have a media player which player mp3 from url. I want to show progress dialog when audio is loading from url. Here is my code.. I am new one so please don't mind if you cannot understand well my question. Thanks in advance

play function

public void  playSong(int naatindex){
    // Play song
    tv = (TextView) this.findViewById(R.id.mywidget);  
    tv.setText("Buffering....");
    tv.setSelected(true); 

    final 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();
    }
    });
    try {

        mp.reset();
        mp.setDataSource(naatpaths[naatindex]);
        tv = (TextView) this.findViewById(R.id.mywidget);  
        tv.setSelected(true);

        mp.prepare();

         // Set focus to the textview
        tv.setText(naattitles[naatindex]);
        // Changing Button Image to pause image
        btnPlay.setImageResource(R.drawable.btn_pause);
        // set Progress bar values
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        // Updating progress bar
        updateProgressBar();            
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

You must use prepareAsync(); and create another textview which text is buffering. while prepareAsync() visible buffering textview. and on prepared complete lister hide buffering textview. I hope that work

public void  playSong(final int naatindex){
// Play song
tv = (TextView) this.findViewById(R.id.mywidget);  
bf = (TextView) this.findViewById(R.id.showbuffering);


try {

mp.reset();
mp.setDataSource(naatpaths[naatindex]);
tv.setVisibility(View.INVISIBLE);
bf.setVisibility(View.VISIBLE);   
mp.prepareAsync();



mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp){
 bf.setVisibility(View.INVISIBLE);
 tv.setVisibility(View.VISIBLE);
 tv.setSelected(true);
 mp.start();

  tv.setText(naattitles[naatindex]);
        tv.setSelected(true);
        // Changing Button Image to pause image
  btnPlay.setImageResource(R.drawable.btn_pause);
  // set Progress bar values
  songProgressBar.setProgress(o);
  songProgressBar.setMax(100);
  // Updating progress bar
  updateProgressBar(); 




}
  });

      // Set focus to the textview

     } catch (IllegalArgumentException e) {
             e.printStackTrace();
      } catch (IllegalStateException e) {
         e.printStackTrace();
         } catch (IOException e) {
       e.printStackTrace();
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM