码迷,mamicode.com
首页 > 移动开发 > 详细

Android SurfaceView播放视频时横竖屏的调整

时间:2015-01-12 14:23:05      阅读:559      评论:0      收藏:0      [点我收藏+]

标签:

对于横屏录制的视频就横屏播放,对于竖屏录制的视频就竖屏播放。

在mainifest文件里对负责播放的Activity添加以下属性

 

    android:configChanges="orientation|keyboardHidden|screenSize"

 

 重写Acitivity的onConfigurationChanged方法:

 

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);

        }

 

根据视频的属性来区分

    @Override
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            if (width == 0 || height == 0) {
                Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                        + ")");
                return;
            }
            Logger.d(TAG, "onVideoSizeChanged width:" + width + " height:" + height);
            mIsVideoSizeKnown = true;
            mVideoHeight = height;
            mVideoWidth = width;
            int wid = mMediaPlayer.getVideoWidth();
            int hig = mMediaPlayer.getVideoHeight();
            // 根据视频的属性调整其显示的模式

            if (wid > hig) {
                if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            } else {
                if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            }
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            mSurfaceViewWidth = dm.widthPixels;
            mSurfaceViewHeight = dm.heightPixels;
            if (width > height) {
                // 竖屏录制的视频,调节其上下的空余

                int w = mSurfaceViewHeight * width / height;
                int margin = (mSurfaceViewWidth - w) / 2;
                Logger.d(TAG, "margin:" + margin);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT);
                lp.setMargins(margin, 0, margin, 0);
                mSurfaceView.setLayoutParams(lp);
            } else {
                // 横屏录制的视频,调节其左右的空余

                int h = mSurfaceViewWidth * height / width;
                int margin = (mSurfaceViewHeight - h) / 2;
                Logger.d(TAG, "margin:" + margin);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT);
                lp.setMargins(0, margin, 0, margin);
                mSurfaceView.setLayoutParams(lp);
            }
        }

 

Android SurfaceView播放视频时横竖屏的调整

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/4218244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!