参考:
VideoView 的使用
demo地址
权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="00:00" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="00:00" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</LinearLayout>
代码:
/**
* VideoView的使用
*/
public class Main2Activity extends Activity {
private VideoView videoView;
private ImageView mIv;
private TextView mProgress;
private TextView totalTime;
private SeekBar seekBar;
private boolean isplay = false;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
private Handler mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (!isplay) {
mProgress.setText(simpleDateFormat
.format(new Date(videoView.getCurrentPosition())));
seekBar.setProgress(videoView.getCurrentPosition());
mhandler.sendEmptyMessageDelayed(0, 10);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
@Override
protected void onDestroy() {
super.onDestroy();
isplay = true;
}
private void initView() {
videoView = (VideoView) findViewById(R.id.video);
mIv = ((ImageView) this.findViewById(R.id.image));
mProgress = ((TextView) this.findViewById(R.id.progress));
totalTime = ((TextView) this.findViewById(R.id.total));
seekBar = ((SeekBar) this.findViewById(R.id.seekbar));
String path = Environment.getExternalStorageDirectory().getPath() +
"/" + "IMG_2948.mp4";
Log.e("Main2Activity", "path=======" + path);
Uri uri = Uri.parse(path);
videoView.setVideoURI(uri);
File file = new File(path);
Bitmap bitmap = initImage(file);
mIv.setImageBitmap(bitmap);
mIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIv.setVisibility(View.GONE);
videoView.start();
}
});
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (videoView.isPlaying()) {
videoView.pause();
} else {
videoView.start();
}
break;
}
return true;
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
videoView.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
totalTime.setText(simpleDateFormat
.format(new Date(videoView.getDuration())));
seekBar.setMax(videoView.getDuration());
mhandler.sendEmptyMessage(0);
}
});
}
/**
* 获取缩略图
*/
private Bitmap initImage(File file) {
MediaMetadataRetriever mediaretriever = new MediaMetadataRetriever();
Bitmap bitmap = null;
try {
mediaretriever.setDataSource(this, Uri.parse(file.getAbsolutePath()));
bitmap = mediaretriever.getFrameAtTime();
return bitmap;
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
} finally {
mediaretriever.release();
}
return null;
}
/**
* 格式化总时长
*/
public String getShowTime(long milliseconds) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliseconds);
SimpleDateFormat dateFormat = null;
if (milliseconds / 60000 > 60) {
dateFormat = new SimpleDateFormat("hh:mm:ss");
} else {
dateFormat = new SimpleDateFormat("mm:ss");
}
return dateFormat.format(calendar.getTime());
}
}