1. 程式人生 > 實用技巧 >Android實現視訊播放功能(VideoView+MediaPlayer)

Android實現視訊播放功能(VideoView+MediaPlayer)

簡介

  此文章記錄自己做視訊播放時遇到的一些問題,最後通過強大的網際網路都解決了,現在作此分享,幫助其他和我一樣的小白快速實現此功能。

效果預覽

    

解決事項

  1、連結網路視訊

  2、修改網路協議

  3、注意XML佈局

  4、根據視訊佈局修改視訊尺寸

  5、實現MediaPlayer在視訊底部

正文

  1、連結連結網路視訊(Activity)

    我做的是兩個視訊播放,所以這裡有兩個VideoView外掛

 1 package com.example.dachuang;
 2 
 3 import android.annotation.SuppressLint;
 4
import android.content.Context; 5 import android.os.Bundle; 6 7 import android.media.MediaPlayer; 8 import android.net.Uri; 9 import android.util.AttributeSet; 10 import android.widget.MediaController; 11 import android.widget.Toast; 12 import android.widget.VideoView; 13 import com.example.dachuang.CustomVideoView;
14 15 import androidx.appcompat.app.AppCompatActivity; 16 17 import static android.view.View.getDefaultSize; 18 19 public class Zhongzhiya_kepu extends AppCompatActivity { 20 21 22 23 private CustomVideoView videoview1 ; 24 private CustomVideoView videoview2 ; 25 26 @Override 27 protected
void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_zhongzhiya_kepu); 30 initUI(); 31 startPlay1(); 32 startPlay2(); 33 } 34 @SuppressLint("WrongViewCast") 35 void initUI(){ 36 videoview1 = (CustomVideoView)findViewById(R.id.v1); 37 videoview2 = (CustomVideoView)findViewById(R.id.v2); 38 39 40 } 41 42 void startPlay1(){ 43 //設定視訊控制器 44 videoview1.setMediaController(new MediaController(this)); 45 //設定網路視訊路徑 46 //String uri = "http://127.0.0.1:8080/test/video.mp4"; 47 String uristr = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; 48 Uri uri = Uri.parse(uristr); 49 videoview1.setVideoURI(uri); 50 videoview1.requestFocus(); 51 videoview1.start(); 52 //播放完成回撥 53 videoview1.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ 54 public void onCompletion(MediaPlayer mp) { 55 Toast.makeText(Zhongzhiya_kepu.this,"播放結束!",Toast.LENGTH_SHORT).show(); 56 } 57 }); 58 59 } 60 void startPlay2(){ 61 //設定視訊控制器 62 videoview2.setMediaController(new MediaController(this)); 63 //設定網路視訊路徑 64 //String uri = "http://127.0.0.1:8080/test/video.mp4"; 65 String uristr = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; 66 Uri uri = Uri.parse(uristr); 67 videoview2.setVideoURI(uri); 68 videoview2.requestFocus(); 69 videoview2.start(); 70 //播放完成回撥 71 videoview2.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ 72 public void onCompletion(MediaPlayer mp) { 73 Toast.makeText(Zhongzhiya_kepu.this,"播放結束!",Toast.LENGTH_SHORT).show(); 74 } 75 }); 76 // videoview2.mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, anchorRect.left, anchorRect.height());; 77 78 }

  2、修改網路協議

     這步很重要,很多小夥伴沒有成功原因就在此(AndroidManifest檔案中)

  此段程式碼與<application.......>是同等級,插錯不會起作用!

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

  3、注意XML佈局 

     VideoView最好在LinearLayout佈局中

  4、根據視訊佈局修改視訊尺寸

  因為佈局的視訊尺寸與網路視訊尺寸不一致,所以為了解決視訊尺寸問題,有了一下程式碼(參考的網上)

 1 package com.example.dachuang;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.view.ViewGroup;
 6 import android.widget.VideoView;
 7 
 8 
 9 public class CustomVideoView extends VideoView {
10     private int mVideoWidth;
11     private int mVideoHeight;
12 
13     public CustomVideoView(Context context) {
14         super(context);
15     }
16 
17     public CustomVideoView(Context context, AttributeSet attrs) {
18         super(context, attrs);
19     }
20 
21     public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
22         super(context, attrs, defStyle);
23     }
24 
25     @Override
26     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
27         /* The following code is to make videoView view length-width
28         based on the parameters you set to decide. */
29         super.onMeasure( widthMeasureSpec,  heightMeasureSpec);
30         int width = getDefaultSize(0, widthMeasureSpec);
31         int height = getDefaultSize(0, heightMeasureSpec);
32         setMeasuredDimension(width, height);
33     }
34 
35 }

  5、實現MediaPlayer在視訊底部

    MediaPlayer默認了,視訊控制器在VideoView佈局的最低處,所以需要在將視訊VideoView單獨安排一個佈局,這裡安排的是LinearLayout

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:orientation="vertical"
 7     tools:context=".Zhongzhiya_kepu">
 8     <RelativeLayout
 9         android:layout_width="match_parent"
10         android:layout_height="71dp"
11         android:background="#fff">
12         <ImageView
13             android:layout_width="60dp"
14             android:layout_height="60dp"
15             android:src="@mipmap/fanhui"
16             android:paddingLeft="10dp"
17             android:paddingTop="20dp">
18         </ImageView>
19         <TextView
20             android:layout_width="150dp"
21             android:layout_height="30dp"
22             android:layout_marginTop="25dp"
23             android:layout_marginLeft="160dp"
24             android:text="科普視訊"
25             android:textSize="22dp"
26             android:textColor="#000000">
27         </TextView>
28         <ImageView
29             android:layout_width="match_parent"
30             android:layout_height="2dp"
31             android:layout_marginTop="68dp"
32             android:background="#D7D7D7">
33         </ImageView>
34     </RelativeLayout>
35 
36     <LinearLayout
37         android:layout_width="match_parent"
38         android:layout_height="match_parent"
39         android:background="#ffffff"
40         android:orientation="vertical">
41             <TextView
42                 android:layout_width="match_parent"
43                 android:layout_height="wrap_content"
44                 android:text="***********"
45                 android:textSize="18dp"
46                 android:textColor="#000"
47                 android:layout_marginTop="10dp"
48                 >
49             </TextView>
50 
51         <LinearLayout
52             android:layout_width="match_parent"
53             android:layout_height="207dp"
54             android:orientation="vertical">
55 
56             <com.example.dachuang.CustomVideoView
57                 android:id="@+id/v1"
58                 android:layout_width="match_parent"
59                 android:layout_height="200dp"
60                 android:layout_marginTop="10dp"></com.example.dachuang.CustomVideoView>
61         </LinearLayout>
62             <TextView
63                 android:layout_width="match_parent"
64                 android:layout_height="wrap_content"
65                 android:text="********"
66                 android:textSize="18dp"
67                 android:textColor="#000"
68                 android:layout_marginTop="10dp"
69                 >
70             </TextView>
71         <LinearLayout
72             android:layout_width="match_parent"
73             android:layout_height="207dp"
74             android:orientation="vertical">
75             <com.example.dachuang.CustomVideoView
76                 android:id="@+id/v2"
77                 android:layout_width="match_parent"
78                 android:layout_height="200dp"></com.example.dachuang.CustomVideoView>
79         </LinearLayout>
80 
81 
82     </LinearLayout>
83 </LinearLayout>

  希望有幫到你哦~

  第一次創作希望得到一個贊 喵~

  有問題留言我看到會恢復的!