Android使用VideoView播放本地视频及网络视频Demo

 1.xm文件

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.zmsoft.TestTool.activity.ZhiboActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <VideoView

            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </RelativeLayout>


</RelativeLayout>
复制代码

  2.写一个类集成  AppCompatActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.zmsoft.TestTool.activity;
 
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
 
import com.zmsoft.TestTool.R;
import com.zmsoft.TestTool.utils.Utils;
 
/**
 * Created by hanbao0928 on 2018/3/30.
 */
 
public class ZhiboActivity extends AppCompatActivity {
 
    private VideoView videoView ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.receice_log);
 
        //本地的视频 需要在手机SD卡根目录添加一个 fl1234.mp4 视频
        String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ;
 
        //网络视频
        String videoUrl2 = Utils.videoUrl ;
 
        Uri uri = Uri.parse( videoUrl2 );
 
        videoView = (VideoView)this.findViewById(R.id.videoView );
 
        //设置视频控制器
        videoView.setMediaController(new MediaController(this));
 
        //播放完成回调
        videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
 
        //设置视频路径
        videoView.setVideoURI(uri);
 
        //开始播放视频
        videoView.start();
    }
 
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
 
        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText( ZhiboActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
        }
    }
}

  3.写一个 Utils  类用于存放网络视频链接地址

1
2
3
4
5
6
7
8
9
10
11
12
package com.zmsoft.TestTool.utils;
 
/**
 * Created by hanbao0928 on 2018/3/30.
 */
 
public class Utils {
 
   // public static final String videoUrl = "http://www.pps.tv/w_19rqylpzsh.mp4" ;
    public static final String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" ;
    //public static final String videoUrl = "http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4" ;
}

  以上则是一个简单的 Android  VideoView 实现本地及其网路视频播放完整Demo,如有疑问请留言,谢谢 !!!

posted @   monkey0928  阅读(769)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示