0

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

3 回答 3

1

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

onMenuItemClick应该已经被 UI 线程调用(UI 元素的回调通常会这样做)。

您应该查看的是使用MediaPlayer.OnPreparedListenerwith VideoView.setOnPreparedListener。这将为您指示视频何时可以播放。

于 2012-04-12T15:44:14.727 回答
0

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

于 2012-05-16T08:53:30.980 回答
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);

}

于 2012-04-12T16:15:02.527 回答