Android原⽣视频播放VideoView的使⽤本⽂实例为⼤家分享了Android原⽣视频播放VideoView的具体代码,供⼤家参考,具体内容如下
布局⽂件l
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/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
ample.administrator.main;
t.Intent;
dia.MediaPlayer;
import android.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) {
setContentView(R.layout.activity_video);
//调⽤系统⾃带视频播放或者安装的第三⽅播放器
// Uri uri=Uri.parse("vd3.bdstatic/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
android layout布局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("vd3.bdstatic/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();
//设置窗⼝模式距离边框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() {
//如果当前页⾯暂停则保存当前播放位置,全局变量保存
intPositionWhenPause = CurrentPosition();
//停⽌回放视频⽂件
videoView.stopPlayback();
}
/**
* 页⾯从暂停中恢复
*/
@Override
protected void onResume() {
//跳转到暂停时保存的位置
if (intPositionWhenPause >= 0) {
videoView.seekTo(intPositionWhenPause);
//初始播放位置
intPositionWhenPause = -1;
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论