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