package com.wuxiaolong.androidsamples.videoplay; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.TextView; import com.wuxiaolong.androidsamples.R; import com.wuxiaolong.videoplayviewlibrary.DensityUtil; import com.wuxiaolong.videoplayviewlibrary.LightnessController; import com.wuxiaolong.videoplayviewlibrary.VideoPlayView; import com.wuxiaolong.videoplayviewlibrary.VolumnController; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class VideoPlayViewActivity extends Activity implements View.OnClickListener, VideoPlayView.onVideoViewStateChangeListener { // 自定义VideoView private VideoPlayView mVideo; // 头部View private View mTopView; // 底部View private View mBottomView; // 视频播放拖动条 private SeekBar mSeekBar; private ImageView mPlay; private TextView mPlayTime; private TextView mDurationTime; // 音频管理器 private AudioManager mAudioManager; // 屏幕宽高 private float width; private float height; // 视频播放时间 private int playTime; private String videoUrl = "http://2449.vod.myqcloud.com/2449_43b6f696980311e59ed467f22794e792.f20.mp4"; // 自动隐藏顶部和底部View的时间 private static final int HIDE_TIME = 5000; // 声音调节Toast private VolumnController volumnController; // 原始屏幕亮度 private int orginalLight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_play_view); initView(); playVideo(); } public void initView() { volumnController = new VolumnController(this); mVideo = (VideoPlayView) findViewById(R.id.videoview); mVideo.setOnVideoViewStateChangeListener(this); mPlayTime = (TextView) findViewById(R.id.play_time); mDurationTime = (TextView) findViewById(R.id.total_time); mPlay = (ImageView) findViewById(R.id.play_btn); mSeekBar = (SeekBar) findViewById(R.id.seekbar); mTopView = findViewById(R.id.top_layout); mBottomView = findViewById(R.id.bottom_layout); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); width = DensityUtil.getWidthInPx(this); height = DensityUtil.getHeightInPx(this); orginalLight = LightnessController.getLightness(this); mPlay.setOnClickListener(this); mSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener); } @Override public void onConfigurationChanged(Configuration newConfig) { Log.d("wxl", "onConfigurationChanged"); if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { height = DensityUtil.getWidthInPx(this); width = DensityUtil.getHeightInPx(this); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { width = DensityUtil.getWidthInPx(this); height = DensityUtil.getHeightInPx(this); } super.onConfigurationChanged(newConfig); } @Override protected void onPause() { super.onPause(); playTime = mVideo.getCurrentPosition(); LightnessController.setLightness(this, orginalLight); } @Override protected void onDestroy() { super.onDestroy(); mHandler.removeMessages(0); mHandler.removeCallbacksAndMessages(null); } @SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: if (mVideo.getCurrentPosition() > 0) { mPlayTime.setText(formatTime(mVideo.getCurrentPosition())); int progress = mVideo.getCurrentPosition() * 100 / mVideo.getDuration(); mSeekBar.setProgress(progress); if (mVideo.getCurrentPosition() > mVideo.getDuration() - 100) { mPlayTime.setText("00:00"); mSeekBar.setProgress(0); } mSeekBar.setSecondaryProgress(mVideo.getBufferPercentage()); } else { mPlayTime.setText("00:00"); mSeekBar.setProgress(0); } break; case 2: showOrHide(); break; default: break; } } }; private void playVideo() { mVideo.setVideoPath(videoUrl); mVideo.requestFocus(); mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mVideo.setVideoWidth(mp.getVideoWidth()); mVideo.setVideoHeight(mp.getVideoHeight()); mVideo.start(); Log.d("wxl", "onPrepared"); if (playTime != 0) { mVideo.seekTo(playTime); } mHandler.removeCallbacks(hideRunnable); mHandler.postDelayed(hideRunnable, HIDE_TIME); mDurationTime.setText(formatTime(mVideo.getDuration())); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { mHandler.sendEmptyMessage(1); } }, 0, 1000); } }); mVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mPlay.setImageResource(R.mipmap.video_btn_down); mPlayTime.setText("00:00"); mSeekBar.setProgress(0); } }); } private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { mHandler.postDelayed(hideRunnable, HIDE_TIME); } @Override public void onStartTrackingTouch(SeekBar seekBar) { mHandler.removeCallbacks(hideRunnable); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Log.d("wxl", "onProgressChanged fromUser=" + fromUser); if (fromUser) { int time = progress * mVideo.getDuration() / 100; mVideo.seekTo(time); } } }; private Runnable hideRunnable = new Runnable() { @Override public void run() { showOrHide(); } }; @SuppressLint("SimpleDateFormat") private String formatTime(long time) { DateFormat formatter = new SimpleDateFormat("mm:ss"); return formatter.format(new Date(time)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play_btn: if (mVideo.isPlaying()) { mVideo.pause(); mPlay.setImageResource(R.mipmap.video_btn_down); } else { mVideo.start(); mPlay.setImageResource(R.mipmap.video_btn_on); } break; default: break; } } /** * 视频进度,后退 * * @param delataX */ @Override public void backward(float delataX) { Log.d("wxl", "backward"); int current = mVideo.getCurrentPosition(); int backwardTime = (int) (delataX / width * mVideo.getDuration()); int currentTime = current - backwardTime; mVideo.seekTo(currentTime); mSeekBar.setProgress(currentTime * 100 / mVideo.getDuration()); mPlayTime.setText(formatTime(currentTime)); } /** * 视频进度,快进 * * @param delataX */ @Override public void forward(float delataX) { Log.d("wxl", "forward"); int current = mVideo.getCurrentPosition(); int forwardTime = (int) (delataX / width * mVideo.getDuration()); int currentTime = current + forwardTime; mVideo.seekTo(currentTime); mSeekBar.setProgress(currentTime * 100 / mVideo.getDuration()); mPlayTime.setText(formatTime(currentTime)); } /** * 声音调小 * * @param delatY */ @Override public void volumeDown(float delatY) { Log.d("wxl", "volumeDown"); int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); int down = (int) (delatY / height * max * 3); int volume = Math.max(current - down, 0); mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); int transformatVolume = volume * 100 / max; volumnController.show(transformatVolume); } /** * 声音调大 * * @param delatY */ @Override public void volumeUp(float delatY) { Log.d("wxl", "volumeUp"); int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); int up = (int) ((delatY / height) * max * 3); int volume = Math.min(current + up, max); mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); int transformatVolume = volume * 100 / max; volumnController.show(transformatVolume); } /** * 灯光减小 * * @param delatY */ @Override public void lightDown(float delatY) { Log.d("wxl", "lightDown"); int down = (int) (delatY / height * 255 * 3); int transformatLight = LightnessController.getLightness(this) - down; LightnessController.setLightness(this, transformatLight); } /** * 灯光加大 * * @param delatY */ @Override public void lightUp(float delatY) { Log.d("wxl", "lightUp"); int up = (int) (delatY / height * 255 * 3); int transformatLight = LightnessController.getLightness(this) + up; LightnessController.setLightness(this, transformatLight); } @Override public void showOrHide() { Log.d("wxl", "showOrHide"); if (mTopView.getVisibility() == View.VISIBLE) { mTopView.clearAnimation(); Animation animation = AnimationUtils.loadAnimation(this, R.anim.option_leave_from_top); animation.setAnimationListener(new AnimationImp() { @Override public void onAnimationEnd(Animation animation) { super.onAnimationEnd(animation); mTopView.setVisibility(View.GONE); } }); mTopView.startAnimation(animation); mBottomView.clearAnimation(); Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.option_leave_from_bottom); animation1.setAnimationListener(new AnimationImp() { @Override public void onAnimationEnd(Animation animation) { super.onAnimationEnd(animation); mBottomView.setVisibility(View.GONE); } }); mBottomView.startAnimation(animation1); } else { mTopView.setVisibility(View.VISIBLE); mTopView.clearAnimation(); Animation animation = AnimationUtils.loadAnimation(this, R.anim.option_entry_from_top); mTopView.startAnimation(animation); mBottomView.setVisibility(View.VISIBLE); mBottomView.clearAnimation(); Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.option_entry_from_bottom); mBottomView.startAnimation(animation1); mHandler.removeCallbacks(hideRunnable); mHandler.postDelayed(hideRunnable, HIDE_TIME); } } private class AnimationImp implements Animation.AnimationListener { @Override public void onAnimationEnd(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } } }