如何使用VideoView Android切换FullScreen

前端之家收集整理的这篇文章主要介绍了如何使用VideoView Android切换FullScreen前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用视频观看直播,我想让这个VideoView切换到全屏并切换到小屏幕,如MXPlayer或YouTube播放器,而不会停止流.
<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ad_container"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" >

        <TextView
            android:id="@+id/scroll_annouc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@color/red"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:lines="1"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="@dimen/ten_dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/scroll_annouc"
            android:background="@android:color/transparent"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="270dp"
                android:background="@android:color/black" >

                <TextView
                    android:id="@+id/error_overlay"
                    android:layout_width="match_parent"
                    android:layout_height="270dp"
                    android:layout_alignParentTop="true"
                    android:background="@color/red_trans"
                    android:gravity="center"
                    android:text="@string/error_text"
                    android:textColor="@android:color/white"
                    android:textStyle="bold"
                    android:visibility="invisible" />

                <VideoView
                    android:id="@+id/player"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true" />

                <RelativeLayout
                    android:id="@+id/media_controller"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:background="@color/black_trans" >

                    <ImageView
                        android:id="@+id/btn_playpause"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:background="@drawable/btn_pause"
                        android:contentDescription="@string/app_name"
                        android:padding="@dimen/five_dp" />

                    <Button
                        android:id="@+id/external_player"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:background="@null"
                        android:text="Use External Player"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/content_size"
                        android:textStyle="bold" />

                    <ImageView
                        android:id="@+id/btn_fullscreen"
                        android:layout_width="40dp"
                        android:layout_height="40dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:contentDescription="@string/app_name"
                        android:padding="@dimen/five_dp"
                        android:src="@drawable/enter_fullscreen" />
                </RelativeLayout>
            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/two_dp"
                android:background="@color/app_blue" />

            <TextView
                android:id="@+id/loading_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingBottom="@dimen/two_dp"
                android:paddingLeft="@dimen/ten_dp"
                android:paddingRight="@dimen/ten_dp"
                android:paddingTop="@dimen/two_dp"
                android:text="@string/app_name"
                android:textColor="@android:color/white"
                android:textSize="@dimen/content_size"
                android:visibility="invisible" />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/one_dp"
                android:background="@color/light_grey" />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/one_dp"
                android:background="@color/light_grey" />

            <LinearLayout
                android:id="@+id/channel_links_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="@dimen/ten_dp" >
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/one_dp"
                android:background="@color/light_grey" />

            <HorizontalScrollView
                android:id="@+id/horizontal_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="horizontal" >

                <LinearLayout
                    android:id="@+id/viewsContainer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </LinearLayout>
            </HorizontalScrollView>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/one_dp"
                android:background="@color/light_grey" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

一切都很好,像流媒体视频和所有,但我无法使这个VideoView去横向和全屏显示视频.我试过谷歌搜索它,但我发现的所有例子目前都不适合我.所以请告诉我如何切换全屏幕.
谢谢.

解决方法

我所做的是我创建了一个CustomVideoView,它扩展了VideoView,如下所示:
public class CustomVideoView extends VideoView {

private int measuredWidth = 0;
private int measuredHeight = 0;

public CustomVideoView(Context context,AttributeSet attrs,int defStyleAttr) {
    super(context,attrs,defStyleAttr);
    // TODO Auto-generated constructor stub
}

public CustomVideoView(Context context,AttributeSet attrs) {
    super(context,attrs);
    // TODO Auto-generated constructor stub
}

public CustomVideoView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public void setNewDimension(int width,int height) {
    this.measuredHeight = height;
    this.measuredWidth = width;
}

@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    setMeasuredDimension(measuredWidth,measuredHeight);
}
}//end class

然后在我的播放器活动中,我在全屏按钮的onclick上实现了这个:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
            android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
    player.setNewDimension(metrics.widthPixels,metrics.heightPixels);
    player.getHolder().setFixedSize(metrics.heightPixels,metrics.widthPixels);
    player.setLayoutParams(params);

所以我的问题解决了.希望它也能解决别人的问题.

注意:我很抱歉没有给任何人信用,因为我忘记了我在谷歌上找到的链接,这导致我为自己制作一个合适的解决方案.

猜你在找的Android相关文章