主页面中
package alice.bw.com.day03surfaceview2.videoview_demo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import alice.bw.com.day03surfaceview2.R;
public class VideoViewActivity extends AppCompatActivity {
private VideoView mVv;
private ImageView mPlayImage;
private SeekBar mSeekBar;
private TextView mTimeText;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 1:
//获取当前视频播放的总时长和当前播放的进度
int allLength = msg.arg1;
int currentPosition = msg.arg2;
//设置显示的时长
mTimeText.setText(currentPosition+" / "+allLength);
//设置seekBar中的数据
mSeekBar.setMax(allLength);
mSeekBar.setProgress(currentPosition);
//循环发送消息, 携带进度
msg = Message.obtain();
msg.arg1 = mVv.getDuration();
msg.arg2 = mVv.getCurrentPosition();
msg.what=1;
if (mVv.isPlaying())
{
handler.sendMessageDelayed(msg,1000);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
initView();
setData();
}
private void initView()
{
mVv = (VideoView) findViewById(R.id.video_view_id);
mPlayImage = (ImageView) findViewById(R.id.video_play_id);
mSeekBar = (SeekBar) findViewById(R.id.seek_bar_id);
mTimeText = (TextView) findViewById(R.id.video_runtime);
}
private void setData()
{
//获取资源的路径
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video_test);
//1, 设置VideoView的视频播放
mVv.setVideoURI(uri);
//2, 开始播放视频
mVv.start();
//为VideoView设置控制器
//设置暂停和播放的按钮
mPlayImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断当前的视频是否正在播放
if (mVv.isPlaying())
{
//由播放到暂停
mVv.pause();
mPlayImage.setImageResource(R.mipmap.play);
}
else
{
handler.removeMessages(1);
//由暂停到播放
mVv.start();
mPlayImage.setImageResource(R.mipmap.pause);
//发送一个消息, 使seekBar , TextView和 VideoView关联
Message message = Message.obtain();
message.arg1 = mVv.getDuration();//得到总时长
message.arg2 = mVv.getCurrentPosition();//得到当前播放的时长
message.what=1;
handler.sendMessageDelayed(message,1000);
}
}
});
mVv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//视频开启播放后, 触发的监听器
Message message = Message.obtain();
message.arg1 = mVv.getDuration();//得到总时长
message.arg2 = mVv.getCurrentPosition();//得到当前播放的时长
message.what=1;
handler.sendMessageDelayed(message,1000);
}
});
//设置进度条拖拽的监听
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
handler.removeMessages(1);
//判断是否是由用户拖拽发生的变化
if (fromUser)
{
//设置VideoView的位置
mVv.seekTo(progress);
}
Message message = Message.obtain();
message.arg1 = mVv.getDuration();//得到总时长
message.arg2 = mVv.getCurrentPosition();//得到当前播放的时长
message.what=1;
handler.sendMessageDelayed(message,1000);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
主页面的布局
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="alice.bw.com.day03surfaceview2.videoview_demo.VideoViewActivity">
<VideoView
android:id="@+id/video_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<!--播放/ 暂停-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/video_play_id"
android:src="@mipmap/pause"
/>
<SeekBar
android:id="@+id/seek_bar_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/horizontalScrollViewStyle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/video_runtime"
android:textColor="@color/colorPrimary"
android:text="123456"/>
</LinearLayout>
</RelativeLayout>
清单文件中要加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />