当前位置:   article > 正文

Android入门:简单视频进度条_videoview自定义进度条

videoview自定义进度条

        继续学习Android,使用监听和消息处理来实现一个简单的视频进度条。

一、效果预览

二、布局

        使用VideoView播放视频,在VideoView底部添加播放进度条,进度条左右分别显示当前播放时间和视频总时间。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content">
  6. <VideoView
  7. android:id="@+id/video"
  8. android:layout_width="match_parent"
  9. android:layout_height="100pt"
  10. android:keepScreenOn="true" />
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="horizontal"
  15. android:layout_alignBottom="@+id/video">
  16. <TextView
  17. android:id="@+id/leftTime"
  18. android:layout_width="0dp"
  19. android:layout_height="match_parent"
  20. android:textColor="#B1AFAF"
  21. android:layout_weight="2"/>
  22. <SeekBar
  23. android:id="@+id/seekbar"
  24. android:layout_width="0dp"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="8"/>
  27. <TextView
  28. android:id="@+id/rightTime"
  29. android:layout_width="0dp"
  30. android:layout_height="match_parent"
  31. android:textColor="#B1AFAF"
  32. android:layout_weight="2"/>
  33. </LinearLayout>
  34. </RelativeLayout>

三、实现

        VideoView可以通过getDuration方法获取视频总长度,调用seekbar.setMax将视频长度与进度条长度同步;设置了总长度后,我们还需要在改变当前进度条进度时,同时改变视频的播放进度,SeekBar可以设置一个监听器,我们需要在进度改变时执行相应的操作。

  1. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  2. @Override
  3. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  4. handler.removeMessages(1);
  5. if(b) {
  6. videoView.seekTo(i);
  7. }
  8. Message message = Message.obtain();
  9. message.arg1 = videoView.getDuration();
  10. message.arg2 = videoView.getCurrentPosition();
  11. message.what=1;
  12. //handler.sendMessageDelayed(message,100);
  13. handler.sendMessage(message);
  14. }
  15. @Override
  16. public void onStartTrackingTouch(SeekBar seekBar) {
  17. }
  18. @Override
  19. public void onStopTrackingTouch(SeekBar seekBar) {
  20. }
  21. });

        这里使用发送消息来同步时间,各个组件的监听中只需要负责发送需要改变的时间和总时间,消息处理中会执行我们定义的方法来更改对应的UI。

四、全部代码

        makeTimeString():实现了对总时间的格式转换,用于显示当前播放时间和总时间。

  1. public class VideoActivity extends AppCompatActivity {
  2. private VideoView videoView;
  3. private SeekBar seekBar;
  4. private TextView leftTime;
  5. private TextView rightTime;
  6. @SuppressLint("HandlerLeak")
  7. private Handler handler = new Handler(){
  8. @Override
  9. public void handleMessage(@NonNull Message msg) {
  10. super.handleMessage(msg);
  11. switch (msg.what){
  12. case 1:
  13. int allLength = msg.arg1;
  14. int currentPosition = msg.arg2;
  15. leftTime.setText(makeTimeString(currentPosition));
  16. rightTime.setText(makeTimeString(allLength));
  17. seekBar.setProgress(currentPosition);
  18. msg = Message.obtain();
  19. msg.arg1 = videoView.getDuration();
  20. msg.arg2 = videoView.getCurrentPosition();
  21. msg.what = 1;
  22. if(videoView.isPlaying()){
  23. handler.sendMessageDelayed(msg,100);
  24. }
  25. break;
  26. }
  27. }
  28. };
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_video);
  33. seekBar=findViewById(R.id.seekbar);
  34. leftTime=findViewById(R.id.leftTime);
  35. rightTime=findViewById(R.id.rightTime);
  36. videoView=findViewById(R.id.video);
  37. //String videoPath = "https:vfx.mtime.cn/Video/2019/07/12/mp4/190712140656051701.mp4";
  38. //videoView.setVideoPath(videoPath);
  39. Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
  40. videoView.setVideoURI(uri);
  41. videoView.start();
  42. videoView.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. if(videoView.isPlaying())
  46. videoView.pause();
  47. else {
  48. handler.removeMessages(1);
  49. videoView.start();
  50. Message message = Message.obtain();
  51. message.arg1 = videoView.getDuration();
  52. message.arg2 = videoView.getCurrentPosition();
  53. message.what=1;
  54. handler.sendMessageDelayed(message,100);
  55. }
  56. }
  57. });
  58. videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  59. @Override
  60. public void onPrepared(MediaPlayer mediaPlayer) {
  61. seekBar.setMax(videoView.getDuration());
  62. Message message = Message.obtain();
  63. message.arg1 = videoView.getDuration();
  64. message.arg2 = videoView.getCurrentPosition();
  65. message.what=1;
  66. handler.sendMessageDelayed(message,100);
  67. }
  68. });
  69. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  70. @Override
  71. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  72. handler.removeMessages(1);
  73. if(b) {
  74. videoView.seekTo(i);
  75. }
  76. Message message = Message.obtain();
  77. message.arg1 = videoView.getDuration();
  78. message.arg2 = videoView.getCurrentPosition();
  79. message.what=1;
  80. //handler.sendMessageDelayed(message,100);
  81. handler.sendMessage(message);
  82. }
  83. @Override
  84. public void onStartTrackingTouch(SeekBar seekBar) {
  85. }
  86. @Override
  87. public void onStopTrackingTouch(SeekBar seekBar) {
  88. }
  89. });
  90. }
  91. //时间转换成字符串,用于显示当前播放时间和总时间
  92. private String makeTimeString(int secs) {
  93. String durationFormat = "%1$02d:%2$02d:%3$02d";
  94. StringBuilder sFormatBuilder = new StringBuilder();
  95. Formatter sFormatter = new Formatter(sFormatBuilder, Locale.getDefault());
  96. sFormatBuilder.setLength(0);
  97. secs = secs / 1000;
  98. Object[] timeArgs = new Object[3];
  99. timeArgs[0] = secs / 3600;
  100. timeArgs[1] = (secs % 3600) / 60;
  101. timeArgs[2] = (secs % 3600 % 60) % 60;
  102. return sFormatter.format(durationFormat,timeArgs).toString().trim();
  103. }
  104. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/499363
推荐阅读
相关标签
  

闽ICP备14008679号