English 中文(简体)
如何雇用ExoPlayer角色 收复函的意见 Destroy
原标题:How to pause ExoPlayer playerView when RecyclerView.SCROLL_STATE_DRAGGING and release player on Destroy

我愿执行《循环录象和录像带》中与“Info”相类似的材料。 我成功地执行了循环器电文,以显示图像和录像制品,但当ScrollListener在上时,我正在对录像物品进行铺设。 RerixrView。 SCROLL_STATE_DRAGGING 我正在利用ExoPlayer图书馆播放录像机。”

因此,如果拍摄了录像,如果循环器的电文不停播放,那么即使从名单上拍摄了另一个录像,该电文也会继续播放。 同时制作多个视频。

下面是我的执行适应方案

public class RecyclerAdapter extends RecyclerView.Adapter {

    private ArrayList<ListModel> listModelList;
    private Context context;

    SimpleExoPlayer exoPlayer;

    public RecyclerAdapter(ArrayList<ListModel> listModelList, Context context) {
        this.listModelList = listModelList;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (listModelList.get(position).getDescription().equalsIgnoreCase("video")) {
            return 1;
        } else {
            return 0;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;

        if (viewType == 0) {
            view = inflater.inflate(R.layout.recycler_item, parent, false);
            return new AdsViewHolder(view);
        } else {
            view = inflater.inflate(R.layout.video_item, parent, false);
            return new VideoVH(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        ListModel listModel = listModelList.get(position);

        if (listModel.getDescription().equalsIgnoreCase("video")) {
            VideoVH videoVH = (VideoVH) holder;
            initPlayer(((VideoVH) holder).playerView, listModel.getMedia_url(), ((VideoVH) holder).progressBar);
            ((VideoVH) holder).titleText.setText(listModel.getTitle());
            ((VideoVH) holder).descText.setText(listModel.getDescription());

            //Play video
        } else {
            AdsViewHolder adsViewHolder = (AdsViewHolder) holder;

            ((AdsViewHolder) holder).titleText.setText(listModel.getTitle());
            ((AdsViewHolder) holder).descText.setText(listModel.getDescription());
            Glide.with(context)
                    .load(listModel.getMedia_url())
                    .into(((AdsViewHolder) holder).imageView);
        }
    }

    private void initPlayer(final PlayerView playerView, String uri, final ProgressBar progressBar) {


        TrackSelector trackSelector = new DefaultTrackSelector();

        exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);

        playerView.setPlayer(exoPlayer);
        exoPlayer.setPlayWhenReady(false);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "ExoRecyclerView"));

        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(uri));

        exoPlayer.prepare(videoSource);

        playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
        exoPlayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
        exoPlayer.seekTo(500);

        exoPlayer.addListener(new Player.EventListener() {
            @Override
            public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {

            }

            @Override
            public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

            }

            @Override
            public void onLoadingChanged(boolean isLoading) {

            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

                switch (playbackState) {

                    case Player.STATE_BUFFERING:
//                        Log.e(TAG, "onPlayerStateChanged: Buffering video.");
                        if (progressBar != null) {
                            progressBar.setVisibility(VISIBLE);
                        }

                        break;
                    case Player.STATE_ENDED:
                        break;
                    case Player.STATE_IDLE:

                        break;
                    case Player.STATE_READY:
//                        Log.e(TAG, "onPlayerStateChanged: Ready to play.");
                        if (progressBar != null) {
                            progressBar.setVisibility(GONE);
                        }
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onRepeatModeChanged(int repeatMode) {

            }

            @Override
            public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

            }

            @Override
            public void onPlayerError(ExoPlaybackException error) {

            }

            @Override
            public void onPositionDiscontinuity(int reason) {

            }

            @Override
            public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

            }

            @Override
            public void onSeekProcessed() {

            }
        });
    }

    @Override
    public int getItemCount() {
        return listModelList.size();
    }

    class AdsViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView titleText, descText;

        public AdsViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.thumbnail);
            descText = itemView.findViewById(R.id.desc);
            titleText = itemView.findViewById(R.id.title);
        }
    }

    class VideoVH extends RecyclerView.ViewHolder {


        PlayerView playerView;
        TextView titleText, descText;
        ConstraintLayout VV;
        ProgressBar progressBar;
        ImageView volumeControl;

        public VideoVH(@NonNull View itemView) {
            super(itemView);

            playerView = itemView.findViewById(R.id.playerView);
            descText = itemView.findViewById(R.id.desc);
            titleText = itemView.findViewById(R.id.title);
            volumeControl = itemView.findViewById(R.id.volume_control);
            VV = itemView.findViewById(R.id.parent);
            progressBar = itemView.findViewById(R.id.progressBar);
        }
    }

}

MainActivity imoplementation

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    ArrayList<ListModel> listModelList = new ArrayList<>();

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

        mRecyclerView = findViewById(R.id.recyclerView);

        ListModel listModel = new ListModel();
        listModelList.add(new ListModel("One test",
                "https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
        listModelList.add(new ListModel("One test",
                "https://res.cloudinary.com/dqrs5xew4/video/upload/v1595538345/HNG/b5bc3f170b7d5c388410bcca0a501ecd_lnhzwn.mp4", "video"));
        listModelList.add(new ListModel("One test",
                "https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
        listModelList.add(new ListModel("One test",
                "https://res.cloudinary.com/dqrs5xew4/video/upload/v1595538331/HNG/VID-20180713-WA0009_esbmff.mp4", "video"));
        listModelList.add(new ListModel("One test",
                "https://res.cloudinary.com/dqrs5xew4/image/upload/v1591991924/sample.jpg", "image"));
        initRecyclerView();
    }

    private void initRecyclerView() {
        // use a linear layout manager
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);

        RecyclerAdapter mAdapter = new RecyclerAdapter(listModelList, MainActivity.this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }
}

示范类

public class ListModel {

    private String title;
    private String media_url;
    private String description;

    public ListModel(String title, String media_url, String description) {
        this.title = title;
        this.media_url = media_url;
        this.description = description;
    }

    public ListModel() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMedia_url() {
        return media_url;
    }

    public void setMedia_url(String media_url) {
        this.media_url = media_url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我在这里和在座程上都经历了几个问题,但是在循环器的电文中,所有这些都只是一个单一的观点类型。

Any suggestion how I could control the Video Player when the recyclerView is been scrolled? Or a better way I could implement the Instagram feeds like RecyclerView

问题回答

/你可以补充这一点,但你应当知道,这并非在所有情况下都行之有效。

Recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            if (adapter != null && adapter.player != null) {
                adapter.player.setPlayWhenReady(false);
            }
        }
    }
});




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签