标签:shel col otto current icon apk san images addview
效果图:
布局:
<?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:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <RelativeLayout android:id="@+id/controllerLayout" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:background="#aa000000"> <ImageView android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingLeft="6dp" android:paddingRight="6dp" android:src="@drawable/icon_play" /> <TextView android:id="@+id/currentTime" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_toRightOf="@id/play" android:gravity="center_vertical" android:paddingLeft="5dp" android:paddingRight="5dp" android:textColor="@color/white" android:textSize="12sp" tools:text="00:12" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="80dp" android:layout_toRightOf="@id/currentTime" android:maxHeight="40dp" android:minHeight="40dp" android:progressDrawable="@drawable/player_setting_bright_progressbar" android:thumb="@drawable/seekbar_button" android:thumbOffset="0dp" /> <ImageView android:id="@+id/changeScreen" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:paddingLeft="6dp" android:paddingRight="6dp" android:src="@drawable/icon_fullscreen" /> <TextView android:id="@+id/totalTime" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_toLeftOf="@id/changeScreen" android:gravity="center_vertical" android:paddingLeft="5dp" android:paddingRight="5dp" android:textColor="@color/white" android:textSize="12sp" tools:text="05:17" /> </RelativeLayout> </RelativeLayout>
java类
public class XueHuVideoView extends RelativeLayout { @BindView(R.id.videoView) VideoView videoView; @BindView(R.id.play) ImageView play; @BindView(R.id.currentTime) TextView currentTime; @BindView(R.id.seekBar) SeekBar seekBar; @BindView(R.id.changeScreen) ImageView changeScreen; @BindView(R.id.totalTime) TextView totalTime; @BindView(R.id.controllerLayout) RelativeLayout controllerLayout; private Timer timer; private String videoName; private Handler mHandler; private SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss"); private String videoPath; private final int FULL = 0; private final int MIN = 1; public XueHuVideoView(Context context) { super(context); initView(context); } public XueHuVideoView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public XueHuVideoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.xuehu_videoview_layout, null); ButterKnife.bind(this, view); addView(view); } private void setVideoListener() { videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { seekBar.setMax(videoView.getDuration()); currentTime.setText("00:00"); totalTime.setText(dateFormat.format(new Date(videoView.getDuration()))); } }); 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.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { videoPause(); } }); } public void videoPause() { if (null != timer) { timer.cancel(); } videoView.pause(); play.setImageResource(R.drawable.icon_play); LoginResponseEntity.Data user = UserData.getUser(); if (null != user) { int currentPosition = videoView.getCurrentPosition(); UserData.saveVideoPlayRecord(String.valueOf(user.getUserId()), videoName.trim(), currentPosition); } } public void videoStart() { if (null != timer) { timer.cancel(); } timer = new Timer(); videoView.start(); play.setImageResource(R.drawable.icon_suspended); timer.schedule(new TimerTask() { @Override public void run() { mHandler.post(new Runnable() { @Override public void run() { seekBar.setProgress(videoView.getCurrentPosition()); currentTime.setText(dateFormat.format(new Date(videoView.getCurrentPosition()))); } }); } }, 0, 1000); } public void setVideoPath(String url) { videoPath = url; videoView.setVideoPath(url); videoName = url.substring(url.lastIndexOf(File.separator) + 1); setVideoListener(); } public void setVideoPlayRecord() { LoginResponseEntity.Data user = UserData.getUser(); if (null != user) { int record = UserData.getVideoPlayRecord(String.valueOf(user.getUserId()), videoName.trim()); if (record > 0) { videoView.seekTo(record); seekBar.setProgress(record); } } } @OnClick({R.id.root_layout, R.id.play, R.id.changeScreen}) public void onClick(View view) { switch (view.getId()) { case R.id.root_layout: controllerLayout.setVisibility(controllerLayout.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); break; case R.id.play: if (videoView.isPlaying()) { videoPause(); } else { videoStart(); } break; case R.id.changeScreen: int tag = (int) (changeScreen.getTag()); switch (tag) { case FULL: VideoPlayFullActivity.start(getContext(), videoPath); break; case MIN: EventBus.getDefault().post(new FullVideoCloseEvent()); break; } break; } } public void setHandler(Handler mHandler) { this.mHandler = mHandler; } public void setChangeScreenMin() { changeScreen.setImageResource(R.drawable.icon_switch_panel); changeScreen.setTag(MIN); } public void setChangeScreenFull() { changeScreen.setImageResource(R.drawable.icon_fullscreen); changeScreen.setTag(FULL); } }
调用(拿全屏播放举例):
public class VideoPlayFullActivity extends Activity { @Autowired String videoPath; @BindView(R.id.videoView) XueHuVideoView videoView; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ARouter.getInstance().inject(this); setContentView(R.layout.activity_video_full); ButterKnife.bind(this); EventBusHelp.register(this); videoView.setHandler(mHandler); videoView.setVideoPath(videoPath); videoView.setChangeScreenMin(); } @Override protected void onStart() { super.onStart(); videoView.setVideoPlayRecord(); videoView.videoStart(); } public static void start(Context context, String videoPath) { Intent starter = new Intent(context, VideoPlayFullActivity.class); starter.putExtra(IntentKey.videoPath, videoPath); if (!(context instanceof Activity)) { starter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(starter); } @Override protected void onPause() { super.onPause(); videoView.videoPause(); } @Override protected void onDestroy() { super.onDestroy(); if (null != mHandler) { mHandler.removeCallbacksAndMessages(null); } } @Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(FullVideoCloseEvent event) { finish(); } }
封装好了就可以复用了
标签:shel col otto current icon apk san images addview
原文地址:http://www.cnblogs.com/anni-qianqian/p/7453519.html