本文实例为大家分享了Android原生视频播放VideoView的具体代码,供大家参考,具体内容如下
布局文件activity_video.xml
<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=".MainActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
对应的Avtivity:VideoActivity.java
package com.example.administrator.main; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.widget.MediaController; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.VideoView; public class VideoActivity extends AppCompatActivity { private ProgressBar progressBar; private VideoView videoView; private MediaController mediaController; private int intPositionWhenPause = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); //调用系统自带视频播放或者安装的第三方播放器 // Uri uri=Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4"); // Intent intent=new Intent(Intent.ACTION_VIEW); // intent.setDataAndType(uri,"video/*"); // startActivity(intent); initVideoView(); } /** * 初始化videoview播放 */ public void initVideoView() { //初始化进度条 progressBar = (ProgressBar) findViewById(R.id.progressBar); //初始化VideoView videoView = (VideoView) findViewById(R.id.videoView); //初始化videoview控制条 mediaController = new MediaController(this); //设置videoview的控制条 videoView.setMediaController(mediaController); //设置显示控制条 mediaController.show(0); //设置播放完成以后监听 videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { } }); //设置发生错误监听,如果不设置videoview会向用户提示发生错误 videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { return false; } }); //设置在视频文件在加载完毕以后的回调函数 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { progressBar.setVisibility(View.GONE); videoView.start(); } }); //设置videoView的点击监听 videoView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; } }); //设置网络视频路径 Uri uri = Uri.parse("http://vd3.bdstatic.com/mda-ig4tp6gnqwu5we8i/mda-ig4tp6gnqwu5we8i.mp4"); videoView.setVideoURI(uri); //设置为全屏模式播放 setVideoViewLayoutParams(2); } /** * 设置videiview的全屏和窗口模式 * * @param paramsType 标识 1为全屏模式 2为窗口模式 */ public void setVideoViewLayoutParams(int paramsType) { //全屏模式 if (1 == paramsType) { //设置充满整个父布局 RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); //设置相对于父布局四边对齐 LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); //为VideoView添加属性 videoView.setLayoutParams(LayoutParams); } else { //窗口模式 //获取整个屏幕的宽高 DisplayMetrics DisplayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(DisplayMetrics); //设置窗口模式距离边框50 int videoHeight = DisplayMetrics.heightPixels; int videoWidth = DisplayMetrics.widthPixels; RelativeLayout.LayoutParams LayoutParams = new RelativeLayout.LayoutParams(videoWidth, videoHeight); //设置居中 LayoutParams.addRule(RelativeLayout.ALIGN_TOP); //为VideoView添加属性 videoView.setLayoutParams(LayoutParams); } } /** * 页面暂停效果处理 */ @Override protected void onPause() { super.onPause(); //如果当前页面暂停则保存当前播放位置,全局变量保存 intPositionWhenPause = videoView.getCurrentPosition(); //停止回放视频文件 videoView.stopPlayback(); } /** * 页面从暂停中恢复 */ @Override protected void onResume() { super.onResume(); //跳转到暂停时保存的位置 if (intPositionWhenPause >= 0) { videoView.seekTo(intPositionWhenPause); //初始播放位置 intPositionWhenPause = -1; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/187612/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)