为什么ProgressDialog不能正常显示?

时间:2012-04-12 15:15:43

标签: android progressdialog

我的ProgressDialog不会显示。在我的程序中,用户从弹出菜单中选择比特率,内容在线获取并显示在VideoView中。

然而,我得到的只是一个黑屏,而视频加载和progressDialog在视频最终加载之前的瞬间显示一瞬间(或更少)。

  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();

  }
用新代码编辑:// @/////////// ////// 现在对话框根本没有显示。

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

3 个答案:

答案 0 :(得分:1)

首先,创建一个单独的线程只是为了在UI线程上运行某些东西是没有意义的(为什么你首先创建线程呢?)。

应该已经由UI线程调用

onMenuItemClick(UI元素的回调通常会这样做)。

您应该注意的是MediaPlayer.OnPreparedListener使用VideoView.setOnPreparedListener。这将告诉您视频何时可以播放。

答案 1 :(得分:0)

我根据Che Jami进行了一些重新安排并让它起作用。感谢

     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);

}

答案 2 :(得分:0)

我有同样的问题...我已经尝试过你的解决方案,但我的进度被阻止......一切都停止了...视频没有显示......似乎onPrepareListener从未被调用过.. 。有任何想法吗?此外,似乎setVideoURI从未传递过代码......:/

我有不同版本的代码...带有线程,带有runOnUI,带有AsyncTask,没有所有内容......似乎没有任何东西可以完全正常工作。总是缺少一些东西。

编辑:

我有onClickListener

中的函数

这是一个像你一样没有任何线程的代码:

版本1:视频在几秒钟后启动,进度对话框只显示视频启动时刻。从一开始就没有显示进展。

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()));

}

第2版带有线程和处理程序...进度对话框显示...它正在旋转...视频显示一秒钟......应用程序崩溃...错误的UI异常......但是当我移动时runOnUiThread中的setVideoURI ...进程永远保持...并且setOnVideoURI永远不会完成。

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

希望你有一些想法...我已经失去了几天......并且无法看到解决方案......对于这个非常基本的事情......它让我发疯:)