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


簡介

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

效果預覽

    

解決事項

  1、鏈接網絡視頻

  2、修改網絡協議

  3、注意XML布局

  4、根據視頻布局修改視頻尺寸

  5、實現MediaPlayer在視頻底部

  6、可能出現的問題

正文

  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     }
 !!!重點!!!
修改AndroidManifest.xml里

 

 <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>

  6、可能出現的問題

7:54 Emulator: [19080:15992:0203/075400.952:ERROR:ssl_client_socket_impl.cc(1050)] handshake failed; returned -1, SSL error code 1, net_error -100

7:54 Emulator: [19080:15992:0203/075401.150:ERROR:ssl_client_socket_impl.cc(1050)] handshake failed; returned -1, SSL error code 1, net_error -100

7:54 Emulator: emulator: INFO: QtLogger.cpp:68: Critical: Uncaught ReferenceError: $ is not defined (qrc:/html/js/location-loader.js:1, (null))

7:54 Emulator: emulator: INFO: QtLogger.cpp:68: Critical: Uncaught ReferenceError: $ is not defined (qrc:/html/js/location-loader.js:1, (null))

         (解決問題)

   

    不要用VideoView組件,換成上面自己定義的組件!!!

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  希望有幫到你哦~

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

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


免責聲明!

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



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