MediaController positioning over VideoView

后端 未结 10 1827
半阙折子戏
半阙折子戏 2020-12-02 07:19

I have a VideoView that takes up the top half of the Activity in portrait orientation with the bottom half of the screen showing some images and text. I am playing a rtsp v

10条回答
  •  长情又很酷
    2020-12-02 07:38

        //It work good with me
    
    
    
    
        
        
    
            
            
    
        
    
    package com.example.videoview;
    
    import android.app.Activity;
    import android.content.res.Configuration;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.media.MediaPlayer.OnVideoSizeChangedListener;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.MediaController;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    public class MainActivity extends Activity {
    
        // private String path = "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4";
        private String path = "http://2387227f13276d2e8940-fbe0b8d9df729a57ca0a851a69d15ebb.r55.cf1.rackcdn.com/hero_2012_demo.mp4";
    
        private VideoView mVideoView;
    
        MediaController mc;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if (mVideoView != null)
                return;
            /**
             * TODO: Set the path variable to a streaming video URL or a local media
             * file path.
             */
    
            mVideoView = (VideoView) findViewById(R.id.videoview);
            mVideoView.setOnCompletionListener(new OnCompletionListener() {
    
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Toast.makeText(MainActivity.this, "The End", Toast.LENGTH_LONG)
                            .show();
                }
            });
    
            if (path == "") {
                // Tell the user to provide a media file URL/path.
                Toast.makeText(
                        MainActivity.this,
                        "Please edit MainActivity, and set path"
                                + " variable to your media file URL/path",
                        Toast.LENGTH_LONG).show();
    
            } else {
                /*
                 * Alternatively,for streaming media you can use
                 * mVideoView.setVideoURI(Uri.parse(path));
                 */
                mVideoView.setVideoPath(path);
    
                mVideoView
                        .setMediaController(new MediaController(MainActivity.this));
                mVideoView.requestFocus();
    
                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.INVISIBLE);
                            }
                        });
                        mVideoView.start();
                    }
                });
    
                mVideoView.setOnTouchListener(new OnTouchListener() {
    
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        if (mc != null) {
                            mc.setVisibility(View.VISIBLE);
                            new Handler().postDelayed(new Runnable() {
    
                                @Override
                                public void run() {
                                    mc.setVisibility(View.INVISIBLE);
                                }
                            }, 2000);
                        }
    
                        return false;
                    }
                });
    
            }
        }
    
        private RelativeLayout.LayoutParams paramsNotFullscreen, paramsFullscreen;
    
        /**
         * handle with the configChanges attribute in your manifest
         */
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            // TODO Auto-generated method stub
            super.onConfigurationChanged(newConfig);
    
            if (paramsFullscreen == null) {
                paramsNotFullscreen = (RelativeLayout.LayoutParams) mVideoView
                        .getLayoutParams();
                paramsFullscreen = new LayoutParams(paramsNotFullscreen);
                paramsFullscreen.setMargins(0, 0, 0, 0);
                paramsFullscreen.height = ViewGroup.LayoutParams.MATCH_PARENT;
                paramsFullscreen.width = ViewGroup.LayoutParams.MATCH_PARENT;
                paramsFullscreen.addRule(RelativeLayout.CENTER_IN_PARENT);
                paramsFullscreen.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                paramsFullscreen.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                paramsFullscreen.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                paramsFullscreen.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            }
            // To fullscreen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mVideoView.setLayoutParams(paramsFullscreen);
    
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                mVideoView.setLayoutParams(paramsNotFullscreen);
            }
        }
    
        @Override
        protected void onPause() {
            if (mVideoView.isPlaying()) {
                mVideoView.pause();
            }
            super.onPause();
        }
    
        @Override
        public void onBackPressed() {
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
            finish();
        }
    }
    
    
    
    
    
        
    
        
        
    
        
            
                
                    
    
                    
                
            
        
    
    
    

提交回复
热议问题