【问题标题】:Android - MediaController of VideoView within dialog appears behind the dialogAndroid - 对话框内VideoView的MediaController出现在对话框后面
【发布时间】:2012-05-17 10:11:01
【问题描述】:

我在自定义对话框中有一个VideoView,我正在为VideoView 即时创建一个媒体控制器,并将其分配给代码中的VideoView,但是控制器实际上并没有出现视频 - 它出现在对话框后面!知道如何将控制器放在视频上方吗?

我创建了一个静态对话框助手类来帮助构建自定义对话框:

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

所以在我的Activity 中,我只需要这个来创建我的对话框:

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

【问题讨论】:

    标签: android dialog android-videoview mediacontroller


    【解决方案1】:

    这对我有用

    在 xml 布局中进行这样的更改,

    <?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>
    

    在你的日志中,

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

    在此,视频视图使用 setOnPreparedListener 监听器并设置媒体控制器,

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

    【讨论】:

    • 别忘了添加 mc.show();在 mc.setVisibility(View.VISIBLE) 之后; ..否则搜索栏和播放按钮状态将不会与播放同步。
    【解决方案2】:

    我建议您需要使用Activity 而不是Dialog。设置您的 Activity 主题以模拟清单中的 Dialog。

    示例 - AndroidManifest.xml:

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

    然后,您将能够像示例中一样显示VideoView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-05
      • 2016-09-21
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多