2012-05-17 46 views
10

Tôi có một VideoView bên trong hộp thoại tùy chỉnh và tôi đang tạo bộ điều khiển phương tiện cho VideoView khi đang di chuyển và gán nó đến VideoView trong mã, tuy nhiên bộ điều khiển không 't thực sự xuất hiện trên video - nó xuất hiện phía sau hộp thoại! Bất kỳ ý tưởng làm thế nào để có được bộ điều khiển trên video?Android - MediaController của VideoView trong hộp thoại xuất hiện phía sau hộp thoại

Tôi tạo ra một lớp helper thoại tĩnh để giúp xây dựng các hộp thoại tùy chỉnh:

public class DialogHelper { 

    public static Dialog getVideoDialog(Context context, Uri videoLocation, boolean autoplay) { 
     final Dialog dialog = getBaseDialog(context,true, R.layout.dialog_video); 

     ((Activity)context).getWindow().setFormat(PixelFormat.TRANSLUCENT); 
     final VideoView videoHolder = (VideoView) dialog.findViewById(R.id.video_view); 
     videoHolder.setVideoURI(videoLocation); 
     //videoHolder.setRotation(90); 
     MediaController mediaController = new MediaController(context); 
     videoHolder.setMediaController(mediaController); 
     mediaController.setAnchorView(videoHolder); 
     videoHolder.requestFocus(); 
     if(autoplay) { 
      videoHolder.start(); 
     } 
     videoHolder.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       dialog.dismiss(); 

      } 
     }); 
     return dialog; 
    } 


    /** 
    * Creates a simple dialog box with as many buttons as you want 
    * @param context The context of the dialog 
    * @param cancelable whether the dialog can be closed/cancelled by the user 
    * @param layoutResID the resource id of the layout you want within the dialog 
    * 
    * @return the dialog 
    */ 
    public static Dialog getBaseDialog(Context context, boolean cancelable, int layoutResID) { 
     Dialog dialog = new Dialog(context, R.style.Theme_PopUpDialog); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setCancelable(cancelable); 
     dialog.setCanceledOnTouchOutside(cancelable); 
     dialog.setContentView(layoutResID); 

     return dialog; 
    } 
} 

Vì vậy, trong tôi Activity tôi chỉ có này để tạo thoại của tôi:

Dialog videoDialog = DialogHelper.getVideoDialog(context, Uri.parse("http://commonsware.com/misc/test2.3gp"), true); 
videoDialog.show(); 

Trả lời

3

tôi đề nghị rằng bạn cần để sử dụng số Activity thay vì Dialog. Đặt chủ đề Activity của bạn để mô phỏng một số Dialog trong tệp kê khai.

Ví dụ - AndroidManifest.xml:

android:theme="@android:style/Theme.Dialog" 

Sau đó, bạn sẽ có thể hiển thị một VideoView như trong ví dụ.

2

này được làm việc cho tôi

trong cách bố trí xml thực hiện thay đổi như thế này,

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <VideoView 
     android:id="@+id/videoview" 
     android:layout_width="640dp" 
     android:layout_height="400dp" 
     android:layout_centerInParent="true" > 
    </VideoView> 

     <FrameLayout 
      android:id="@+id/videoViewWrapper" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" > 
     </FrameLayout> 

    </RelativeLayout> 

trong dilog của bạn,

mVideoView = (VideoView) view.findViewById(R.id.videoview); 

trong này, video xem sử dụng setOnPreparedListener nghe và thiết lập truyền thông bộ điều khiển,

mVideoView.setOnPreparedListener(new OnPreparedListener() { 

       @Override 
       public void onPrepared(MediaPlayer mp) { 
        // TODO Auto-generated method stub 
        mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() { 
         @Override 
         public void onVideoSizeChanged(MediaPlayer mp, 
           int width, int height) { 
          /* 
          * add media controller 
          */ 
          mc = new MediaController(MainActivity.this); 
          mVideoView.setMediaController(mc); 
          /* 
          * and set its position on screen 
          */ 
          mc.setAnchorView(mVideoView); 

          ((ViewGroup) mc.getParent()).removeView(mc); 

          ((FrameLayout) findViewById(R.id.videoViewWrapper)) 
            .addView(mc); 
          mc.setVisibility(View.VISIBLE); 
         } 
        }); 
        mVideoView.start(); 
       } 
      }); 
Các vấn đề liên quan