seekbar with preview,like youtube

时间:2018-01-13 04:26:44

标签: android youtube seekbar

我需要一些组件或github链接来显示视频的搜索栏(或进度条),并在设置的时间显示视频的缩略图(由搜索栏设置) (像youtube)

非常感谢!

2 个答案:

答案 0 :(得分:1)

我认为您在寻找SeekBar时应该使用此GitHub库来预览图像

https://github.com/rubensousa/PreviewSeekBar

希望这有助于你...如果你需要任何帮助,你可以问

答案 1 :(得分:0)

好的,我想出了自己,然后我上传到:

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);
    }
}

在父母中你只需添加,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();
相关问题