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); //綁定視頻視圖控件ID 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM