xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:keepScreenOn="true"
android:background="@color/colorBlack1"
tools:context=".work.share.VideoPlayActivity"> <SurfaceView
android:id="@+id/video_play_surfaceview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/> <ImageView
android:id="@+id/start_and_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_sex_male"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/vedio_current_time"
app:layout_constraintBottom_toBottomOf="parent" /> <TextView
android:id="@+id/vedio_current_time"
android:text="当前时间"
android:textColor="@color/fontWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toTopOf="@id/start_and_stop"
app:layout_constraintBottom_toBottomOf="@id/start_and_stop"
app:layout_constraintLeft_toRightOf="@id/start_and_stop"
app:layout_constraintRight_toLeftOf="@id/vedio_seek"/> <SeekBar
android:id="@+id/vedio_seek"
android:layout_width="0dp"
android:layout_height="0dp"
android:maxHeight="3dp"
android:minHeight="3dp"
android:progressDrawable="@drawable/vedio_seekbar_bg"
app:layout_constraintTop_toTopOf="@id/start_and_stop"
app:layout_constraintBottom_toBottomOf="@id/start_and_stop"
app:layout_constraintLeft_toRightOf="@id/vedio_current_time"
app:layout_constraintRight_toRightOf="@id/vedio_total_time"/> <TextView
android:id="@+id/vedio_total_time"
android:textColor="@color/fontWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:layout_constraintTop_toTopOf="@id/start_and_stop"
app:layout_constraintBottom_toBottomOf="@id/start_and_stop"
app:layout_constraintLeft_toRightOf="@id/vedio_seek"
app:layout_constraintRight_toRightOf="parent"/> <ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="@dimen/title_height"
android:paddingLeft="20dp"
android:src="@mipmap/ic_back_white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/> <TextView
android:id="@+id/vedio_settings"
android:textColor="@color/fontWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>

代码

/**
*@content:视频播放activity
*@time:2019-6-13 */ public class VideoPlayActivity extends BaseActivity implements View.OnClickListener {
private static final String TAG = VideoPlayActivity.class.getSimpleName();
private ConstraintLayout mRootLayout;
private SurfaceView mVideoPlaySurfaceview;
private ImageView mStartAndStop, mBack;
private MediaPlayer mMediaPlayer;
private SeekBar mVedioSeek;
private TextView mVedioCurrentTimeTextView, mVedioTotalTimeTextView, mVedioSettings;
private String mPath;
private boolean isInitFinish = false;
private SurfaceHolder mSurfaceHolder;
private Handler mHandler;
private final int GET_VIDEO_PLAY_TIME_KEY = 0x01;
private final int GONS_VIEW_KEY = 0x02;
private static int mCurrentPlayTime = 0; //因为切换横竖屏后,activity其实是重新创建的,所以需要用静态变量保存当前播放时间
private int mTotalPlayTime = 0;
SimpleDateFormat mTimeFormat = new SimpleDateFormat("mm:ss"); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
initVedioSeekListener();
initHandler();
mHandler.sendEmptyMessageDelayed(GONS_VIEW_KEY,8*1000);
File file = new File(getExternalCacheDir(), "demo.mp4");
mPath = file.getAbsolutePath();
initMediaPalyer();
initSurfaceviewStateListener(); } @Override
protected void onPause() {
super.onPause();
pausePlay();
} @Override
protected void onResume() {
super.onResume();
startPlay();
} @Override
public int getLayout() {
return R.layout.activity_video_play;
} @Override
public void initView() {
mRootLayout = findViewById(R.id.root_layout);
mVideoPlaySurfaceview = findViewById(R.id.video_play_surfaceview);
mStartAndStop = findViewById(R.id.start_and_stop);
mVedioCurrentTimeTextView = findViewById(R.id.vedio_current_time);
mVedioTotalTimeTextView = findViewById(R.id.vedio_total_time);
mVedioSeek = findViewById(R.id.vedio_seek);
mVedioSettings = findViewById(R.id.vedio_settings);
mBack = findViewById(R.id.back);
mStartAndStop.setOnClickListener(this);
mBack.setOnClickListener(this);
mRootLayout.setOnClickListener(this); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_and_stop:
if (mMediaPlayer.isPlaying()) {
pausePlay();
mStartAndStop.setImageResource(R.mipmap.ic_sex_male);
} else {
startPlay();
mStartAndStop.setImageResource(R.mipmap.ic_sex_female);
}
break;
case R.id.back:
finish();
break;
case R.id.root_layout:
mStartAndStop.setVisibility(View.VISIBLE);
mVedioSettings.setVisibility(View.VISIBLE);
mVedioTotalTimeTextView.setVisibility(View.VISIBLE);
mVedioCurrentTimeTextView.setVisibility(View.VISIBLE);
mBack.setVisibility(View.VISIBLE);
mVedioSeek.setVisibility(View.VISIBLE);
mHandler.sendEmptyMessageDelayed(GONS_VIEW_KEY,8*1000);
break;
default:
break;
} } private void initHandler(){
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case GET_VIDEO_PLAY_TIME_KEY:
if (mMediaPlayer != null){
mCurrentPlayTime = mMediaPlayer.getCurrentPosition();
L.e("每秒mCurrentPlayTime="+mCurrentPlayTime);
mVedioSeek.setProgress(mCurrentPlayTime/1000);
mVedioCurrentTimeTextView.setText(mTimeFormat.format(mCurrentPlayTime)); }
mHandler.sendEmptyMessageDelayed(GET_VIDEO_PLAY_TIME_KEY,1000);
break;
case GONS_VIEW_KEY:
mStartAndStop.setVisibility(View.GONE);
mVedioSettings.setVisibility(View.GONE);
mVedioTotalTimeTextView.setVisibility(View.GONE);
mVedioCurrentTimeTextView.setVisibility(View.GONE);
mBack.setVisibility(View.GONE);
mVedioSeek.setVisibility(View.GONE);
break;
default:
break; }
}
};
} private void initVedioSeekListener(){
mVedioSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMediaPlayer.seekTo(seekBar.getProgress()*1000);
mCurrentPlayTime = mMediaPlayer.getCurrentPosition();
mVedioCurrentTimeTextView.setText(mTimeFormat.format(mCurrentPlayTime)); }
});
} private void initSurfaceviewStateListener() {
mSurfaceHolder = mVideoPlaySurfaceview.getHolder();
mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer.setDisplay(holder);//给mMediaPlayer添加预览的SurfaceHolder
setPlayVideo(mPath);//添加播放视频的路径
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.e(TAG, "surfaceChanged触发: width=" + width + "height" + height); } @Override
public void surfaceDestroyed(SurfaceHolder holder) { }
});
} private void initMediaPalyer() {
mMediaPlayer = new MediaPlayer(); } private void setPlayVideo(String path) {
try {
mMediaPlayer.setDataSource(path);//
mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);//缩放模式
mMediaPlayer.setLooping(true);//设置循环播放
mMediaPlayer.prepareAsync();//异步准备
// mMediaPlayer.prepare();//同步准备,因为是同步在一些性能较差的设备上会导致UI卡顿
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { //准备完成回调
@Override
public void onPrepared(MediaPlayer mp) {
isInitFinish = true;
mMediaPlayer.start();
mHandler.sendEmptyMessage(GET_VIDEO_PLAY_TIME_KEY);
mTotalPlayTime = mMediaPlayer.getDuration();
if (mTotalPlayTime == -1){
Toast.makeText(VideoPlayActivity.this, "视频文件时间异常", Toast.LENGTH_SHORT).show();
finish();
}
mVedioSeek.setMax(mTotalPlayTime/1000);
mVedioTotalTimeTextView.setText(formatTime(mTotalPlayTime));
}
});
mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() { //尺寸变化回调
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
changeVideoSize();
L.e("mCurrentPlayTime="+mCurrentPlayTime);
seekTo(mCurrentPlayTime); }
});
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
L.e("视频播放错误,错误原因what="+what+"extra="+extra);
return false;
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) { }
});
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 修改视频的大小,以用来适配屏幕
*/
public void changeVideoSize() {
int videoWidth = mMediaPlayer.getVideoWidth();
int videoHeight = mMediaPlayer.getVideoHeight();
int deviceWidth = getResources().getDisplayMetrics().widthPixels;
int deviceHeight = getResources().getDisplayMetrics().heightPixels;
Log.e(TAG, "changeVideoSize: deviceHeight="+deviceHeight+"deviceWidth="+deviceWidth);
float devicePercent = 0;
//下面进行求屏幕比例,因为横竖屏会改变屏幕宽度值,所以为了保持更小的值除更大的值.
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { //竖屏
devicePercent = (float) deviceWidth / (float) deviceHeight; //竖屏状态下宽度小与高度,求比
}else { //横屏
devicePercent = (float) deviceHeight / (float) deviceWidth; //横屏状态下高度小与宽度,求比 } if (videoWidth > videoHeight){
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
videoWidth = deviceWidth;
videoHeight = (int)(videoHeight/devicePercent); }else {
videoWidth = deviceWidth;
videoHeight = (int)(deviceWidth*devicePercent);
} }else {
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {//竖屏 videoHeight = deviceHeight;
float videoPercent = (float) videoWidth / (float) videoHeight;
float differenceValue = Math.abs(videoPercent - devicePercent);
if (differenceValue < 0.3){
videoWidth = deviceWidth;
}else {
videoWidth = (int)(videoWidth/devicePercent);
} }else { //横屏
videoHeight = deviceHeight;
videoWidth = (int)(deviceHeight*devicePercent); } } ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) mVideoPlaySurfaceview.getLayoutParams();
layoutParams.width = videoWidth;
layoutParams.height = videoHeight;
layoutParams.verticalBias = 0.5f;
layoutParams.horizontalBias = 0.5f;
mVideoPlaySurfaceview.setLayoutParams(layoutParams); } private void startPlay(){
if (!mMediaPlayer.isPlaying()){
mMediaPlayer.start();
}
} private void stopPlay(){
if (mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
}
} private void pausePlay(){
if (mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
} private void seekTo(int time){
mMediaPlayer.seekTo(time);
} private String formatTime(long time){
return mTimeFormat.format(time);
} @Override
protected void onDestroy() {
super.onDestroy();
if (mMediaPlayer != null){
if (mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
}
mMediaPlayer.release();
mMediaPlayer = null;
}
} }

Android开发 MediaPlayer播放本地视频完善的demo(只是代码记录)的更多相关文章

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

    1.xm文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  2. Android 使用意图播放本地视频

    Android播放视频的方式有三种: 一.使用意图播放,调用本地安装的播放器,选择一个进行播放. 二.使用VideoView播放(VideoView事实上是对MediaPlayer的封装,使用起来非常 ...

  3. Android开发之获取本地视频和获取自拍视频

    1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...

  4. Android开发 MediaPlayer播放raw资源MP3文件

    代码 private MediaPlayer mRingPlayer; /** * 播放铃声 */ private void startRing(){ if (mRingPlayer != null) ...

  5. Android开发 MediaPlayer入门_播放本地视频

    前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...

  6. 照相、从相册上取照片、播放音频、播放本地视频、播放网络视频、MPMoviePlayerController

    一.照相.从相册上去照片 1. 先判断是否支持照相功能 *判断当前设备是否支持照相功能,支持返回YES 否则返回NO 注意:模拟器不支持照相功能 把握一个原则只要是物理硬件相关的功能模拟器都不支持 例 ...

  7. Swift - 使用Media Player播放本地视频,在线视频

    Media Player框架用于播放本地视频.音频,也可以在线播放视频和音频. 1,播放器MPMovieControlStyle样式有如下几种: (1)None: 没有播放控制控件 (2)Embedd ...

  8. potplayer 网页调用potplayer播放本地视频

      网页调用potplayer播放本地视频 CreateTime--2018年1月3日10:36:24 Author:Marydon 源码展示: <!DOCTYPE html> <h ...

  9. Android 视频播放器 VideoView 的使用,播放本地视频 和 网络 视频

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

随机推荐

  1. maven环境变量配置及测试

    首先要确认,你是否已经安装了jdk,并且已经成功配置了环境变量. 和java配置类似,计算机右键属性->高级系统设置->环境变量. 添加如下变量: 变量名:maven_home 变量值:C ...

  2. caffer的三种文件类别

    solver文件 是一堆超参数,比如迭代次数,是否用GPU,多少次迭代暂存一次训练所得参数,动量项,权重衰减(即正则化参数),基本的learning rate,多少次迭代打印一次loss,以及网络结构 ...

  3. [转载]MinGW安装过程

    本文转自https://blog.csdn.net/my_wade/article/details/46941645 MinGW安装过程 一. 下载 MinGW官网下载地址:http://source ...

  4. CSS 是怎样确定图像大小的?

    本文转自奇舞周刊学习使用侵权删 先来看个例子,热热身. 上面这张图像的原始尺寸是:宽 54px 高 49px. 那么,在以下代码中,每张图像显示的最终尺寸是多少? https://p1.ssl.qhi ...

  5. Array Beauty

    Array Beauty 给出一个长度为n的序列\(\{a_i\}\),定义一个序列的权值为其中元素两两之差的绝对值的最小值,询问\(\{a_i\}\)长度为K的子序列的权值之和\(\% 998244 ...

  6. 谈谈你对本次2018级ACM新手赛的体会

    第一次参加这类比赛,挺有趣的,在现场磨了四个小时也没有全写出来,收获还是挺大的,至少意识到自己是能做到这些的(笑 今后也会多多努力

  7. 深入理解MAGENTO – 第九章 – 数据集合瓦瑞恩

    本来,作为一个PHP程序员,如果你想攒一组变量的相关你有一个选择,古老的 阵列 . 尽管共享一个地址的名称与C存储器的阵列,一个PHP数组是一种通用的字典可变数组索引像数值对象结合行为的影响. 在其他 ...

  8. idea关联git后 Git上传项目提示Push rejected: Push to origin/master was rejected解决办法

    当所有的东西都配好以后  就是不上数据  解决方案是在所属右键 点击Git BashHere后  输入:git pull origin master –allow-unrelated-historie ...

  9. 生产环境用到的几个有用的Linux命令

    有时候,几个有用的Linux命令可以很大的提高你的工作效率. 1.free -m 这个命令我暂时就只会这么使用,它可以查看服务器的内存资源 2.top 这个命令同样可以查看服务器的资源,当然我还是用它 ...

  10. Python_day01——变量

    变量 1.声明变量   name="钱成龙"  变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 关键字不能声明为变量名 2.变量类型 整 ...