android - 带有预览的搜索栏,例如 youtube

标签 android youtube seekbar

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

非常感谢!

最佳答案

好吧,我想通了,我上传到:

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

关于android - 带有预览的搜索栏,例如 youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48236910/

相关文章:

android - 使用单例类改造动态基 url 更改

android - 电话间隙 : Timeout error calling webviewclient

ios - 获取 Youtube 视频 viewCount Youtube API v3/Swift

youtube - Youtube SDK-如何在PHP中获得Analytics Reporting访问和数据转储?

api - 有没有一种方法可以通过YouTube API v3设置不喜欢和不喜欢的评论?

python - 如何处理 hildon.Seekbar 中的后退和前进按钮?

android - SeekBarPreference/NumberPickerPreference ClassCastException 异常

android Mediarecorder 通过蓝牙发送视频流

android - 如何通过短信发送非打印字符

android - Mediaplayer 进度更新到 seekbar 不顺畅?