Android Studio:在MediaView中为VideoView创建全屏按钮

时间:2015-07-21 15:41:15

标签: android fullscreen android-videoview

我一直在互联网上搜索如何在我的媒体控制器中为我的视频创建一个全屏按钮。我已经看到一些网站显示如何做到这一点,但大多数方法我无法理解,因为我是android studio的新手。我已经能够将媒体控制器添加到我的应用程序中,而不是使用全屏按钮。因此,当您在媒体播放器中单击它时,它会从屏幕的一半进入全屏,然后在全屏时,当您单击相同的按钮时,它会变回。我知道这需要一个自定义的媒体控制器,但我已经尝试过我在网上看到的大多数,但它们不起作用。请帮忙

这是我的篮球投篮xml     

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="?colorPrimary"/>

<android.support.v7.widget.Toolbar
    android:layout_marginTop="25dp"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@drawable/action_bar_color"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ToolbarTheme" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="80dp"
    android:layout_gravity="bottom">

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost"
        android:layout_gravity="center_horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/action_bar_color"
                android:elevation="4dp"> </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <LinearLayout
                    android:id="@+id/Video"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:background="@color/md_divider"
                    android:layout_gravity="left">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/VideoAndTitle"
                        android:layout_margin="7dp">

                        <LinearLayout
                            android:id="@+id/VideoHolder"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:background="#FFFFFF"
                            android:layout_weight="50">

                            <VideoView
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:id="@+id/videoView"
                                android:elevation="2dp" />

                        </LinearLayout>

                        <LinearLayout
                        android:id="@+id/VideoTitle"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:background="#FFFFFF"
                            android:layout_weight="1">

                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:id="@+id/video_title"
                                android:text="Basketball Shooting Tutorial"
                                android:textSize="16sp"
                                android:layout_gravity="start"
                                android:paddingTop="5dp"
                                android:paddingLeft="5dp"
                                android:paddingBottom="5dp" />

                        </LinearLayout>
                    </LinearLayout>


                </LinearLayout>



                <LinearLayout
                    android:id="@+id/Description"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:showDividers="none"
                    android:background="@color/md_divider">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#FFFFFF"
                        android:layout_margin="10dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:text="Description"
                            android:id="@+id/textView3"
                            android:textSize="17sp"
                            android:textStyle="bold"
                            android:padding="5dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="Small Text"
                            android:id="@+id/textView4"
                            android:textSize="12sp"
                            android:layout_marginTop="10dp"
                            android:padding="5dp" />
                    </LinearLayout>
                </LinearLayout>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这是我的BasketballShooting.java

package com.jehan.sportstutorials;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TabHost;
import android.widget.VideoView;
import android.media.MediaPlayer.OnPreparedListener;
import android.app.ProgressDialog;
import android.util.Log;
import android.media.MediaPlayer;



public class BasketballShooting extends AppCompatActivity {

Toolbar toolbar;
ProgressDialog pDialog;
VideoView videoview;

String VideoURL = "https://ia801507.us.archive.org/13/items/basketball_shooting.3gp/basketball_shooting.mp4";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_basketball_shooting);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    TypedValue typedValueColorPrimaryDark = new TypedValue();
    BasketballShooting.this.getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValueColorPrimaryDark, true);
    final int colorPrimaryDark = typedValueColorPrimaryDark.data;
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().setStatusBarColor(colorPrimaryDark);
    }

    TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
    tabHost.setup();

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("video");
    tabSpec.setContent(R.id.Video);
    tabSpec.setIndicator("Video");
    tabHost.addTab(tabSpec);


    tabSpec = tabHost.newTabSpec("description");
    tabSpec.setContent(R.id.Description);
    tabSpec.setIndicator("Description");
    tabHost.addTab(tabSpec);

    //VideoView
    videoview = (VideoView) findViewById(R.id.videoView);

    // Create a progressbar
    pDialog = new ProgressDialog(BasketballShooting.this);
    // Set progressbar title
    pDialog.setTitle("Basketball Shooting Tutorial");
    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar
    pDialog.show();

    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                BasketballShooting.this);
        mediacontroller.setAnchorView(videoview);
        // Get the URL from String VideoURL
        Uri video = Uri.parse(VideoURL);
        videoview.setMediaController(mediacontroller);
        videoview.setVideoURI(video);

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    videoview.requestFocus();
    videoview.setOnPreparedListener(new OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            pDialog.dismiss();
            videoview.start();
        }
    });


}

@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_basketball_shooting, 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_bar) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

由于我是Android的新手,因此非常感谢简化的解释。

0 个答案:

没有答案