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

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  类用于存放网络视频链接地址

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,如有疑问请留言,谢谢 !!!

上一篇:ldap 集成harbor


下一篇:python全栈开发中级班全程笔记(第二模块、第四章(三、re 正则表达式))