android关于VideoView或Vitamio视频播放器横竖屏切换

之前在网上寻求帮助时,留有QQ,好多人加我问过这个问题,现在为了方便大家,我将android关于VideoView或Vitamio视频播放器横竖屏切换的代码放在这里需要的朋友可以看一下,当然我做的这个也是从网上得来的,然后稍作修改,当然我做的这个也有很多问题,希望大家指点(这个demo只是videoview,导入vitamio也是可以的)。

代码如下:

1.androidManifest.xml里面设置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.helloworld.android.videoplayer" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"

        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

2.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="0dp"
    tools:context=".MainActivity">

    <FrameLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/idshowed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <VideoView
                    android:id="@+id/videoView"
                    android:layout_width="match_parent"
                    android:layout_height="250dip"
                    android:layout_centerInParent="true" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_margin="0dip"
            android:layout_weight="1"
            android:gravity="right"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/fullornormalscreen"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:layout_gravity="right"
                android:layout_marginBottom="0dp"
                android:layout_marginRight="0dp"
                android:background="#f0000000"

                android:textColor="#ffffff" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>



3.MainActivity里面 的代码

public class MainActivity extends Activity {

    private ImageView fullornormalscreen;
    private boolean fullscreen = false;
    private VideoView mVideoView;
    private static int vWidth;
    private static int vHeight;
    private boolean displayScreen;
    private LinearLayout idshowed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displayScreen = true;
        mVideoView = (VideoView) findViewById(R.id.videoView);
     //   Uri uri = Uri.parse("http://wslive-hls.kascend.com/chushou_live/545f32f8dccf4e85b49875418d42d33c/playlist.m3u8");
        mVideoView.setVideoPath("http://video19.ifeng.com/video09/2016/07/17/4144324-102-008-1057.mp4");
        //mVideoView.setVideoPath("http://cache.m.iqiyi.com/dc/dt/text/7565bc03x2f443409x777a6178/20161111/e2/73/8a155c81ea5663e5c925f93e1dcdd49b.m3u8?np_tag=nginx_part_tag&qd_sc=ce230a3dccc84e298cd146af45445d0d&t_sign=-0-02020031010000000000-566766500_04000000001000000000_1");
        mVideoView.start();
        MediaController mediaController;
        mediaController = new MediaController(this);
        mediaController.setVisibility(View.GONE);
        mVideoView.setMediaController(mediaController);
        idshowed = (LinearLayout)this.findViewById(R.id.idshowed);
        fullornormalscreen = (ImageView) findViewById(R.id.fullornormalscreen);
        fullornormalscreen.setImageDrawable(getResources().getDrawable(R.drawable.icon_full));
        fullornormalscreen.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View view){
                String path = "";
                new MyClickTask().execute(path);
            }
        });
        idshowed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(displayScreen){
                    fullornormalscreen.setVisibility(View.INVISIBLE);
                    displayScreen = false;
                }else{
                    fullornormalscreen.setVisibility(View.VISIBLE);
                    displayScreen = true;
                }
                System.out.println("---------------------Show--Hide---------------");
            }
        });
    }
    public class MyClickTask extends AsyncTask<String,Integer,String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected String doInBackground(String... params) {
            String path = params[0];
            String title;
            if (!fullscreen) {//设置RelativeLayout的全屏模式

                //   System.out.println("-----linearLayout_player_nba----false------->>:1" );
                MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                System.out.println("-----------------asyncTask--------->:1");
                title = "窗口";
                //   LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)linearLayout_player_nba.getLayoutParams();
                fullscreen = true;//改变全屏/窗口的标记
            } else {//设置RelativeLayout的窗口模式

                MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                System.out.println("-----------------asyncTask--------->:2");
                title = "全屏";
                fullscreen = false;//改变全屏/窗口的标记
            }

            return title;
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if(result == "窗口")
                fullornormalscreen.setImageDrawable(getResources().getDrawable(R.drawable.icon_normal));
            else
                fullornormalscreen.setImageDrawable(getResources().getDrawable(R.drawable.icon_full));
        }
    }
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检测屏幕的方向:纵向或横向
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //当前为横屏, 在此处添加额外的处理代码
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);//消除状态栏

            System.out.println("-----------------onConfigurationChanged--------->:1");

            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);

            int width = dm.widthPixels;
            int height = dm.heightPixels;
            vWidth = width;
            ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
            lp.width = width;
            lp.height = height;

            mVideoView.setLayoutParams(lp);
       //     getWindow().getDecorView().setSystemUiVisibility(View.INVISIBLE);//显示状态栏

        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏, 在此处添加额外的处理代码
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            System.out.println("-----------------onConfigurationChanged--------->:2");

            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);

            int width = dm.widthPixels;
            int height = dm.heightPixels;
            vWidth = width;
            ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
            lp.width = width;
            lp.height = (int) (height * (1 - 0.618));

            mVideoView.setLayoutParams(lp);
            getWindow().getDecorView().setSystemUiVisibility(View.VISIBLE);//显示状态栏

        }


        //检测实体键盘的状态:推出或者合上
        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
            //实体键盘处于推出状态,在此处添加额外的处理代码
        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
            //实体键盘处于合上状态,在此处添加额外的处理代码
        }


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最后布局里面用到两张图片


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值