啓動頁面播放視頻

啓動頁面播放視頻

啓動頁面播放視頻超簡單實現方案:

  • studio中的res目錄下創建raw文件夾
  • 將3gp或mp4格式的視頻文件放入

代碼部分

主要佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:padding="10dp"
        android:layout_marginLeft="100dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:text="開始體驗"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:background="@drawable/bg_border"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

主要代碼文件:

public class Videoplayer extends Activity {
    public static final String TAG = "Videoplayer";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 全屏顯示
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.aty_video);
        /*主要代碼起始位置*/
        final VideoView vv = (VideoView) this.findViewById(R.id.videoView);
        final String uri = "android.resource://" + getPackageName() + "/" + R.raw.loader;

        vv.setVideoURI(Uri.parse(uri));
        vv.start();
        vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);

            }
        });

        vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        vv.setVideoURI(Uri.parse(uri));
                        vv.start();

                    }
                });
                /*主要代碼結束位置*/
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章