Android SeekBar controls video playback progress (1)

Android SeekBar controls video playback progress (1)

renderings

insert image description here

Introduction

When using VideoViewthe controls to play a video, we hope to be able to adjust the progress of the playback. One way is to use the built-in controller MediaControllerto control, and the other way is to implement a SeekBarcontrol by ourselves.

use MediaControllercontroller

MediaControllerUse the built-in controller to control the progress while playing the video .

MediaController mediaController = new MediaController(this);
mVideoView.setMediaController(mediaController);

insert image description here

useSeekBar

Use yourself to implement a control playback progress when playing a video SeekBar.
insert image description here

  1. Customize SeekBarthe background drawable/bg_rounded.xmlto achieve rounded semi-transparent effects.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 背景颜色 -->
    <solid android:color="#20ffffff"/>

    <!-- 圆角背景 -->
    <corners
        android:topLeftRadius="12dp"
        android:topRightRadius="12dp"
        android:bottomLeftRadius="12dp"
        android:bottomRightRadius="12dp"/>

</shape>
  1. Interface layout file , the interface has a control layout/activity_video.xmlfor video playback and a control for controlling the playback progress. The background effect defined in the first step is used, android:background="@drawable/bg_rounded"VideoViewSeekBar
    SeekBar
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:keepScreenOn="true"
    tools:context=".activitys.VideoActivity">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="500dp"
        android:layout_height="25dp"
        android:background="@drawable/bg_rounded"
        android:layout_marginBottom="45dp"
        android:progressTint="#7FFFD4"
        android:thumbTint="#7FFFD4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
        
</androidx.constraintlayout.widget.ConstraintLayout>
  1. VideoActivitydocument.
public class VideoActivity extends AppCompatActivity implements EdgeSensorView.MapDemo {
    
    

    private static final String TAG = "VideoActivity";

    private VideoView mVideoView;
    private SeekBar mSeekBar;

    private float curProgress;
    private float MAX_PROGRESS;

    private boolean isSeekBarProgress = false;

    private Handler handler = new Handler();

	// 播放过程,自动更新seekbar的进度
    private Runnable runnable = new Runnable() {
    
    

        public void run() {
    
    

            if (mVideoView.isPlaying()) {
    
    
                if (!isSeekBarProgress) {
    
    
                    int current = mVideoView.getCurrentPosition();
                    mSeekBar.setProgress(current);
                }
            }
            handler.postDelayed(runnable, 100);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        mSeekBar = findViewById(R.id.seekbar);
        mVideoView = findViewById(R.id.video_view);
        mVideoView.setVideoURI(Uri.parse("android.resource://"
                + getApplicationContext().getPackageName() + "/" + R.raw.vid_bigbuckbunny));
//        MediaController mediaController = new MediaController(this);
//        mVideoView.setMediaController(mediaController);

        mVideoView.requestFocus();

        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    
    
            @Override
            public void onPrepared(MediaPlayer mp) {
    
    
                mp.setLooping(true);

                MAX_PROGRESS = mVideoView.getDuration();
                mSeekBar.setMax((int) MAX_PROGRESS);

                Log.i(TAG, "onCreate: " + MAX_PROGRESS + "," + curProgress + "," + bitmaps.length);

                // 开始线程,更新进度条的刻度
                handler.postDelayed(runnable, 0);
                mVideoView.start();
            }
        });

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    
    
            @Override
            public void onCompletion(MediaPlayer mp) {
    
    
                curProgress = 0;
                mSeekBar.setProgress(0);
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    
    

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
    
                isSeekBarProgress = true;
                if (mVideoView.isPlaying()) {
    
    
                    mVideoView.pause();
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
    
                int pro = seekBar.getProgress();
                mVideoView.seekTo(pro);
                if (!mVideoView.isPlaying()) {
    
    
                    mVideoView.seekTo(pro);
                    mVideoView.start();
                }
                isSeekBarProgress = false;
            }
        });

    }

    @Override
    protected void onResume() {
    
    
        super.onResume();
    }

    @Override
    protected void onPause() {
    
    
        super.onPause();
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }

    private void updateSeekBarStatus(final boolean b) {
    
    
        runOnUiThread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                mSeekBar.setPressed(b);
            }
        });
    }
}

expand:

  1. Add a button to control play/pause;
  2. Increase the text display of the current playing time and total duration of the playing video;

Guess you like

Origin blog.csdn.net/tracydragonlxy/article/details/129878705