Android如何制作一个简易的视频播放器

 

Android如何制作一个简易的视频播放器

 

      ——安德风QQ1652102745

 

一、效果演示:

 

 

 

二、布局设计activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:layout_width="match_parent"
 7     android:orientation="vertical"
 8     android:layout_height="match_parent"
 9     tools:context="com.example.video.MainActivity">
10 
11     <VideoView
12         android:id="@+id/videoView"
13         android:layout_width="match_parent"
14         android:layout_height="300dp" />
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal">
19         <Button
20             android:id="@+id/btn_start"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="开始"
24             android:layout_marginLeft="20dp"/>
25 
26         <Button
27             android:id="@+id/btn_end"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="结束" />
31     </LinearLayout>
32 </LinearLayout>

三、功能实现MainActivity.java

 1 package com.example.video;
 2 
 3 
 4 import android.net.Uri;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.MediaController;
 9 import android.widget.VideoView;
10 
11 import androidx.appcompat.app.AppCompatActivity;
12 
13 public class MainActivity extends AppCompatActivity {
14     private VideoView videoView;
15     private Button btn_start,btn_end;
16     private MediaController mediaController;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         initView();
23     }
24 
25     private void initView() {
26         videoView= (VideoView) findViewById(R.id.videoView);
27         btn_start= (Button) findViewById(R.id.btn_start);
28         btn_end= (Button) findViewById(R.id.btn_end);
29 
30 
31         btn_start.setOnClickListener(new View.OnClickListener() {
32             @Override
33             public void onClick(View v) {
34                 init();
35             }
36         });
37         btn_end.setOnClickListener(new View.OnClickListener() {
38             @Override
39             public void onClick(View v) {
40                 videoView.stopPlayback();
41             }
42         });
43     }
44 
45     private void init() {
46         videoView = (VideoView) findViewById(R.id.videoView);
47         mediaController = new MediaController(this);
48         String uri = "android.resource://" + getPackageName() + "/" + R.raw.a;
49         videoView.setVideoURI(Uri.parse(uri));
50         videoView.setMediaController(mediaController);
51         mediaController.setMediaPlayer(videoView);
52         videoView.requestFocus();
53         videoView.start();
54     }
55 }

四、视频存放路径:R/raw

猜你喜欢

转载自www.cnblogs.com/adf520/p/12945759.html