一、序言
公司的智能设备要添加个视频播放功能,不要求在线播放,而且视频也比较小,所以就不采用在线播放的一些库,简单的写个本地播放器~~功能比较简单直接上代码,
二、播放原理
使用的是MediaPlayer +SurfaceView,由于本地VideoView也是使用,但是对于视频格式有比较大的限制,所以采用MediaPlayer,稍微比较麻烦一些。
三、播放代码
- 页面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<SurfaceView
android:id="@+id/svVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/tvBack"
android:layout_width="match_parent"
android:layout_height="120px"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/shape_video_top"
android:drawableLeft="@drawable/video_back"
android:gravity="center"
android:paddingLeft="30px" />
<RelativeLayout
android:id="@+id/rlVideoBottom"
android:layout_width="match_parent"
android:layout_height="120px"
android:layout_alignParentBottom="true"
android:background="@drawable/shape_video_bottom"
android:visibility="visible">
<ImageView
android:id="@+id/ivVideoControl"
android:layout_width="56px"
android:layout_height="56px"
android:layout_centerVertical="true"
android:layout_marginLeft="60px"
android:src="@drawable/video_play_s" />
<TextView
android:id="@+id/tvVideoStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="54px"
android:layout_toRightOf="@+id/ivVideoControl"
android:text="00:00:00"
android:textColor="@color/white"
android:textSize="28px" />
<SeekBar
android:id="@+id/sbVideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30px"
android:layout_marginRight="30px"
android:layout_toLeftOf="@+id/tvVideoEnd"
android:layout_toRightOf="@id/tvVideoStart" />
<TextView
android:id="@+id/tvVideoEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="54px"
android:layout_marginRight="30px"
android:text="00:00:00"
android:textColor="@color/white"
android:textSize="28px" />
</RelativeLayout>
<ImageView
android:id="@+id/ivVideoControlBig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/video_play"
android:visibility="visible" />
</RelativeLayout>
- 播放代码
package com.gzjiequan.rescuewithyou.rescuesys.sosdevice;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import com.gzjiequan.rescuewithyou.R;
import com.gzjiequan.rescuewithyou.base.BaseActivity;
import com.gzjiequan.rescuewithyou.util.DateUtil;
import com.gzjiequan.rescuewithyou.util.FileAccessor;
import com.gzjiequan.rescuewithyou.util.LogUtil;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import java.io.File;
import java.io.IOException;
import java.util.TimeZone;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class VideoPlayerActivity extends BaseActivity {
@BindView(R.id.svVideo)
SurfaceView svVideo;
@BindView(R.id.tvBack)
TextView tvBack;
@BindView(R.id.ivVideoControl)
ImageView ivVideoControl;
@BindView(R.id.tvVideoStart)
TextView tvVideoStart;
@BindView(R.id.sbVideo)
SeekBar sbVideo;
@BindView(R.id.tvVideoEnd)
TextView tvVideoEnd;
@BindView(R.id.rlVideoBottom)
RelativeLayout rlVideoBottom;
@BindView(R.id.ivVideoControlBig)
ImageView ivVideoControlBig;
private String videoUrl = "";
private int allTime, currentTime;
private CountDownTimer countDownTimer;
private MediaPlayer mediaPlayer;
private File videoFile;
@Override
protected int getContentViewId() {
return R.layout.activity_video_player;
}
@Override
protected void initView(Bundle savedInstanceState) {
videoUrl = getIntent().getStringExtra(this.getClass().getSimpleName());
if (!TextUtils.isEmpty(videoUrl)) {
onStartProgress();
videoFile = new File(FileAccessor.FILE + getIntent().getStringExtra("NAME") + FileAccessor.MP4);
mediaPlayer=new MediaPlayer();
if(!videoFile.exists()) {
// 下载视频,可以用其他方式下载
FinalHttp finalHttp = new FinalHttp();
finalHttp.download(videoUrl, videoFile.getAbsolutePath(), new AjaxCallBack<File>() {
@Override
public void onSuccess(File file) {
super.onSuccess(file);
// 开始加载视频
svVideo.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
startVideo();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
}
});
}else {
svVideo.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
startVideo();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
}
}
public void startVideo() {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(videoFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setDisplay(svVideo.getHolder());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
allTime = mp.getDuration() - TimeZone.getDefault().getRawOffset();
tvVideoEnd.setText(DateUtil.getVideoTime(allTime));
sbVideo.setMax(mediaPlayer.getDuration() + 10);
mediaPlayer.seekTo(sbVideo.getProgress() + 10);
LogUtil.d(TAG, " onPrepared" + mp.getDuration());
onStopProgress();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
ivVideoControlBig.setVisibility(View.VISIBLE);
ivVideoControl.setImageResource(R.drawable.video_play_s);
rlVideoBottom.setVisibility(View.VISIBLE);
tvBack.setVisibility(View.VISIBLE);
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
});
mediaPlayer.prepareAsync();
countDownTimer = new CountDownTimer(24 * 60 * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
currentTime = mediaPlayer.getCurrentPosition() - TimeZone.getDefault().getRawOffset();
tvVideoStart.setText(DateUtil.getVideoTime(currentTime));
sbVideo.setProgress(mediaPlayer.getCurrentPosition());
LogUtil.d(TAG, "getCurrentPosition" + mediaPlayer.getCurrentPosition());
}
@Override
public void onFinish() {
}
};
sbVideo.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) {
LogUtil.d(TAG, "onStopTrackingTouch " + seekBar.getProgress());
mediaPlayer.seekTo(seekBar.getProgress());
tvVideoStart.setText(DateUtil.getVideoTime(mediaPlayer.getCurrentPosition() - TimeZone.getDefault().getRawOffset()));
}
});
}
@Override
protected void onDestroy() {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer=null;
}
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}
@OnClick({R.id.tvBack, R.id.ivVideoControl, R.id.svVideo})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.tvBack:
finish();
break;
case R.id.svVideo:
if (mediaPlayer == null) {
return;
}
if (mediaPlayer.isPlaying()) {
ivVideoControlBig.setVisibility(View.VISIBLE);
ivVideoControl.setImageResource(R.drawable.video_play_s);
rlVideoBottom.setVisibility(View.VISIBLE);
tvBack.setVisibility(View.VISIBLE);
mediaPlayer.pause();
if (countDownTimer != null) {
countDownTimer.cancel();
}
} else {
ivVideoControlBig.setVisibility(View.GONE);
ivVideoControl.setImageResource(R.drawable.video_stop_s);
rlVideoBottom.setVisibility(View.GONE);
tvBack.setVisibility(View.GONE);
mediaPlayer.start();
if (countDownTimer != null) {
countDownTimer.start();
}
}
break;
case R.id.ivVideoControl:
if (mediaPlayer == null) {
return;
}
if (mediaPlayer.isPlaying()) {
ivVideoControlBig.setVisibility(View.VISIBLE);
ivVideoControl.setImageResource(R.drawable.video_play_s);
rlVideoBottom.setVisibility(View.VISIBLE);
tvBack.setVisibility(View.VISIBLE);
mediaPlayer.pause();
if (countDownTimer != null) {
countDownTimer.cancel();
}
} else {
ivVideoControlBig.setVisibility(View.GONE);
ivVideoControl.setImageResource(R.drawable.video_stop_s);
rlVideoBottom.setVisibility(View.GONE);
tvBack.setVisibility(View.GONE);
mediaPlayer.start();
if (countDownTimer != null) {
countDownTimer.start();
}
}
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: add setContentView(...) invocation
ButterKnife.bind(this);
}
}
网友评论