Search code examples
androidyoutubeseekbar

seekbar with preview, like youtube


I need some component or github link to show a seekbar (or progress bar) of a video, and show a thumbnail of the video at the time set (set by the seekbar) (like youtube)

Thanks a lot!


Solution

  • Ok, I Figured out myself, and I uploaded to:

    https://github.com/CorradiSebastian/SeekBarPreview

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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"
        tools:context="com.sebastiancorradi.seekbar.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Coded By Sebastian Corradi SebastianCorradi@gmail.com"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:textColor="#AAAAAA"
            android:textSize="10dp"
            app:layout_constraintTop_toTopOf="parent" />
    
        <com.sebastiancorradi.seekbar.components.SeekBarPreview
            android:id="@+id/seekBarPreviewSDCard"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>
    
    
    </android.support.constraint.ConstraintLayout>
    

    .JAVA:

    public class SeekBarPreview  extends LinearLayout {
        private SeekBar mSeekBar;
        private ImageView mPreview;
        private View rootView;
    
        private int totalDuration;
        private int currentPosition;
        private MediaMetadataRetriever retriever;
    
        public SeekBarPreview(Context context) {
            super(context);
            init();
        }
    
        public SeekBarPreview(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            init();
        }
    
        public SeekBarPreview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        private void init(){
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rootView = inflater.inflate(R.layout.seekbarpreview_layout, this, true);
    
            mPreview = rootView.findViewById(R.id.imageView);
            mSeekBar = rootView.findViewById(R.id.seekBar);
            retriever = new MediaMetadataRetriever();
    
            mPreview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
    
        public void setUrl(String url){
            retriever.setDataSource(url, new HashMap<String, String>());
            String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            if (duration != null) {
                totalDuration = Integer.valueOf(duration);//milisecs
            } else {
                totalDuration = 0;
                //or throw an exception
            }
        }
        public void setUri(Uri uri){
            retriever.setDataSource(getContext(),uri);
            String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            if (duration != null) {
                totalDuration = Integer.valueOf(duration);//milisecs
            } else {
                totalDuration = 0;
                //or throw an exception
            }
        }
    
        public void update(int i) {
            Long newPosition = totalDuration * i * 10L;
            Bitmap bitmap = retriever.getFrameAtTime(newPosition);
            mPreview.setImageBitmap(bitmap);
    
    
            int x = mSeekBar.getThumb().getBounds().centerX();
            int newPos = x - (i * mPreview.getWidth() / 100);
            mPreview.setX(newPos);
    
    
        }
    
        public void initialize(){
    
            mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    update(i);
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            });
            mSeekBar.setProgress(1);
        }
    }
    

    And in the parent you just have to add, MXL:

    <com.sebastiancorradi.seekbar.components.SeekBarPreview
        android:id="@+id/seekBarPreviewSDCard"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>
    

    JAVA:

    SeekBarPreview seekBarPreviewSDCard = findViewById(R.id.seekBarPreviewSDCard);
    
            String path = "android.resource://" + getPackageName() + "/" + R.raw.bunny;
            seekBarPreviewSDCard.setUri(Uri.parse(path));
    
            seekBarPreviewSDCard.initialize();