简体   繁体   中英

Why won't ProgressDialog show properly?

My ProgressDialog will not show up. In my program the user selects bitrate from a pop up menu, the content is fetched online and displayed in VideoView.

However, all I get is a black screen while the video loads and the progressDialog is display for a split second (or less) at end before the video finally loads.

  private void showPopupMenu(View v){
   popupMenu = new PopupMenu(VideoPlayer.this, v); 
   if(bitRates != null){
       int menuItem = 0;
       int index = bitRates.size()-1;
       popupMenu.getMenu().add(0,menuItem, 0,"Hide menu");
          for(int i=1;i<bitRates.size();i++){
              menuItem = i;

              popupMenu.getMenu().add(0,menuItem, 0,"Quality"+" : "+bitrateCheck(bitRates.get(index))).setCheckable(true);
              Log.i("ITEM",qualityList.get(i)+" : "+bitRates.get(i));
              index--;
          }
          popupMenu.getMenu().add(0,menuItem++, 0,"Catalog");

      }
  popupMenu.show(); 
  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

   public boolean onMenuItemClick(final MenuItem item) {
        p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading...");

       //get current position of video to seek to
       final int position = mVideoView.getCurrentPosition();
       new Thread() {

            public void run() {

                try{

                    runOnUiThread(new Runnable() {
                        public void run() {

                            if(item.getTitle().equals("Catalog")){
                                 backButtonVideo(new View(VideoPlayer.this));
                            }
                            else if(item.getTitle().equals("Hide menu")){
                                popupMenu.dismiss();
                                mVideoView.start();                             
                            }
                            else{
                                play(streamUrls.get(item.getItemId()),position);

                            }
                        }
                    });


                }
                catch (Exception e) {
                    Log.e("tag", e.getMessage());

                }
                p_dialog.dismiss();
            }

        }.start();

       return true;

   }
  });

}


 public void play(String _videoUri, int position){
         Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());         
         //View view = new View()
         MediaController mc = new MediaController(VideoPlayer.this);
         mVideoView.setMediaController(mc); 
         mVideoView.setVideoURI(Uri.parse(_videoUri));
         mVideoView.requestFocus();
         mVideoView.seekTo(position);
         mVideoView.start();

  }

////////////////////////Edit with new Code: as per suggestions///////////////////// Now the dialog does not show at all.

     mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub  
        mVideoView.start();

    }
});

///////////////////////////////////////////////////////////////////////////////////

  popupMenu.show(); 
  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

   public boolean onMenuItemClick(final MenuItem item) {       
       //get current position of video to seek to
       final int position = mVideoView.getCurrentPosition();
       p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading...");
       try{
            if(item.getTitle().equals("Catalog")){
                backButtonVideo(new View(VideoPlayer.this));
            }
            else if(item.getTitle().equals("Hide menu")){
                popupMenu.dismiss();
                mVideoView.start();                             
            }
            else{
                play(streamUrls.get(item.getItemId()),position);                            
            }

        }
        catch (Exception e) {Log.e("tag", e.getMessage());}  
       return true;  
   }
  });

 }


  public void play(String _videoUri, int position){
         Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());         
         MediaController mc = new MediaController(VideoPlayer.this);
         mVideoView.setMediaController(mc); 
         mVideoView.setVideoURI(Uri.parse(_videoUri));
         mVideoView.requestFocus();
         mVideoView.seekTo(position);
         p_dialog.dismiss();
  }

Firstly, it makes no sense to create a separate thread only to run something back on the UI thread (why do you create the thread in the first place then?).

onMenuItemClick should already be called by the UI thread (callbacks for UI elements generally do this).

What you should look at is using a MediaPlayer.OnPreparedListener with VideoView.setOnPreparedListener . This will give you an indication of when the video is ready to be played.

I did some rearrangement as per Che Jami and got it to work. Thanks

     mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {    
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        p_dialog.dismiss();
        mVideoView.start();


    }
});

////////////////////////////////////////////////////////////////////////////////

      popupMenu.show(); 
  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

   public boolean onMenuItemClick(final MenuItem item) {       
       //get current position of video to seek to
       final int position = mVideoView.getCurrentPosition();

       try{
            if(item.getTitle().equals("Catalog")){
                backButtonVideo(new View(VideoPlayer.this));
            }
            else if(item.getTitle().equals("Hide menu")){
                popupMenu.dismiss();
                mVideoView.start();                             
            }
            else{
                p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading new stream");
                play(streamUrls.get(item.getItemId()),position);                            
            }

        }
        catch (Exception e) {Log.e("tag", e.getMessage());}  
       return true;  
   }
  });

}

  public void play(String _videoUri, int position){
         Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());



     MediaController mc = new MediaController(VideoPlayer.this);
     mVideoView.setMediaController(mc); 
     mVideoView.setVideoURI(Uri.parse(_videoUri));
     mVideoView.requestFocus();
     mVideoView.seekTo(position);

}

I have the same problem...i've tried you solution but my progress gets blocked...everything stops...and video doesn't show up...it seems that onPrepareListener is never called up...any ideas? Also it seems that setVideoURI has never passed in code...:/

I have different versions of code...with threads, with runOnUI, with AsyncTask, without everything...nothing seems to be working fully. something is always missing.

EDIT:

I have function that I'm calling in onClickListener

Here is a code like yours without any threads:

Version 1: Video starts after few seconds, progress dialog just show for a moment when video starts. Progress isn't showed from the beginning.

public void loadService(final int mode) {

    //Version 1

    // Show dialog
    loadService = new ProgressDialog(activity);

    loadService.setTitle("Loading service");
    loadService.setMessage("Please wait...");
    loadService.setCancelable(false);
    loadService.show();

    // Hide progress dialog after 15 seconds if nothing happens
    DuoTVGuiHandler.progressBarProttection(loadService, 15);

    // Setup video view for playback
    if (mode == STAND_ALONE) {
        setupVideoView(((DuoTVGuiActivity) activity).getVideoPlayback());
    }

    if (mode == WIDGET_MODE) {
        setupVideoView(((MainActivity) activity).getVideoViewWidgetMode());
    }
}

/** Setup video view for playback */
public void setupVideoView(final VideoView videoView) {

    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            loadService.dismiss();
            videoView.start();
        }
    });

    videoView.setVideoURI(Uri.parse(channels.get(index).getFilePath()));

}

Version 2 with thread and handler...progress dialog is showed...its spinning...video shows for a second...and the app crashes...WrongThread for UI exception...but when i move setVideoURI in runOnUiThread...progress just stays forever...and setOnVideoURI is never finished.

public void loadService(final int mode) {

    // //////////////////////////////
    // Version 2
    // //////////////////////////////

    // Show dialog
    loadService = new ProgressDialog(activity);

    loadService.setTitle("Loading service");
    loadService.setMessage("Please wait...");
    loadService.setCancelable(false);
    loadService.show();

    final Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            loadService.dismiss();

            super.handleMessage(msg);
        }
    };

    Thread loadVideoPath = new Thread(new Runnable() {

        public void run() {
            if (mode == STAND_ALONE) {
                ((DuoTVGuiActivity) activity)
                        .getVideoPlayback()
                        .setVideoURI(
                                Uri.parse(channels.get(index).getFilePath()));
            }

            if (mode == WIDGET_MODE) {
                ((MainActivity) activity)
                        .getVideoViewWidgetMode()
                        .setVideoURI(
                                Uri.parse(channels.get(index).getFilePath()));
            }

            handler.sendEmptyMessage(0);
        }
    });

    loadVideoPath.start();
}

Hope you have some idea...I've lost few days on this...and cant see solution...for this pretty basic thing...it drives me crazy :)

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