我正在构建一个简单的应用程序,将视频从firebase流式传输到我的应用程序,就像tiktok一样。为此,我使用exoplayer,但exoplayer同时播放所有视频,并且在viewpager2中滑动时不会停止。
我的应用程序问题视频-https://youtu.be/tjq6w4zdfee
这是我的viewpageradapter代码-
public myviewholder(@NonNull View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
title = (TextView) itemView.findViewById(R.id.textVideoTitle);
desc = (TextView) itemView.findViewById(R.id.textVideoDescription);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
}
void setdata(final Post obj) {
title.setText(obj.getCaption());
desc.setText(obj.getCaption());
fabvideo.setLabelText(obj.getPostid());
simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
playerView = findViewById(R.id.exoPlayerView);
playerView.setPlayer(simpleExoPlayer);
MediaItem mediaItem = MediaItem.fromUri(obj.getVideourl);
simpleExoPlayer.addMediaItem(mediaItem);
simpleExoPlayer.prepare();
simpleExoPlayer.setPlayWhenReady(true);
}
下面是我使用viewpager2的片段代码-
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_newsorted,container,false);
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
ViewPager2 viewPager2 = (ViewPager2) v.findViewById(R.id.vpager);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Posts");
FirebaseRecyclerOptions<Post> options =
new FirebaseRecyclerOptions.Builder<Post>()
.setQuery(query, Post.class)
.build();
adapter=new Likedsortedadapter(options,getContext());
viewPager2.setAdapter(adapter);
return v;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
暂无答案!
目前还没有任何答案,快来回答吧!