首页 文章

Android媒体控制器在一个对话框中

提问于
浏览
0

我在对话框中显示VideoView,我正在附加媒体控件 .

但是当我尝试点击媒体控件(播放,搜索栏等)时,对话框就会被取消 .

媒体控制按钮不会被轻敲,而是轻敲注册为对话的OutsideTouch .

谁能帮我这个?

我真正想要实现的目标如下:

1)在背景模糊的弹出窗口中显示视频 . 2)检测VideoView外的任何水龙头并弹出“取消??”消息给用户 .

2 回答

  • 0

    对话框布局如下所示,

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

    在您的Dilalog Frgament中制作视频视图的实例,

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

    接下来从本地设置视频uri或使用视频网址播放,之后,使用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();
                    }
                });
    
  • 1

    我找到了一种不同的方式来实现我想要的 .

    我将视频视图放在一个单独的活动中,并将清单中的活动主题设置为

    android:theme =“@ style / Theme.Transparent”

    并在res / values / styles.xml中添加了以下内容

    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/transparent1</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
      </style>
    

    接下来,我重写了onTouchEvent函数,如下所示:

    @Override
        public boolean onTouchEvent(MotionEvent event) {
    
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
    
                int[] l = new int[2];
                v.getLocationInWindow(l);
                int x = l[0];
                int y = l[1];
                int w = v.getWidth();
                int h = v.getHeight();
    
                if ((int) event.getX() > x && (int) event.getX() < (x + w)
                        && (int) event.getY() > y && (int) event.getY() < (y + h)) {
                    // touch is inside the videoview
    
                } else {
                    // touch is outside the videoview
                }
    
                break;
            case MotionEvent.ACTION_MOVE:
    
                break;
            case MotionEvent.ACTION_UP:
    
                break;
            }
    
            return false;
        }
    

    其中“v”是视频视图 .

相关问题