'how to play one video in view pager with exo player
i am making a app where i am playing videos with exo player in view pager but problem is that i am not able play one video at a time. all videos playing there at a time .i am not unable to pause videos on swipe.please help.
**this is my activity where i am setting data in view pager**
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),list);
viewpager.setAdapter(adapter);
this is my view pager
public class ViewPagerAdapter extends FragmentPagerAdapter {
List<VideosModel> list;
public ViewPagerAdapter(FragmentManager fragmentManager, List<VideosModel> list) {
super(fragmentManager);
this.list=list;
}
// Returns total number of pages
@Override
public int getCount() {
return list.size();
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
return GlobalFragment.newInstance(list.get(position).getVideo());
}
}
this is my fragment where i am getting video url and play in exo player
public class GlobalFragment extends Fragment {
public static String video_url;
public static ExoPlayer player;
private PlayerView my_player_view;
public GlobalFragment() {
// Required empty public constructor
}
// newInstance constructor for creating fragment with arguments
public static GlobalFragment newInstance(String title) {
GlobalFragment fragmentFirst = new GlobalFragment();
Bundle args = new Bundle();
args.putString(Commn.video_url,title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
video_url=getArguments().getString(Commn.video_url);
Log.e("Videoss","Videoss"+video_url+"");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_global, container, false);
iniViews(view);
setDimension();
playVideo();
return view;
}
private void iniViews(View view) {
my_player_view=view.findViewById(R.id.my_player_view);
my_player_view.setRepeatToggleModes(10);
}
private void playVideo() {
try {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
player = ExoPlayerFactory.newSimpleInstance(getActivity(), trackSelector);
Uri videoURI = Uri.parse(video_url);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);
my_player_view.setPlayer(player);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}catch (Exception e){
Log.e("MainAcvtivity"," exoplayer error "+ e.toString());
}
}
private void setDimension() {
float videoProportion = getVideoProportion();
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
float screenProportion = (float) screenHeight / (float) screenWidth;
android.view.ViewGroup.LayoutParams lp = my_player_view.getLayoutParams();
if (videoProportion < screenProportion) {
lp.height= screenHeight;
lp.width = (int) ((float) screenHeight / videoProportion);
} else {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth * videoProportion);
}
my_player_view.setLayoutParams(lp);
}
private float getVideoProportion(){
return 2f;
}
Solution 1:[1]
int pager fragment layouts I define this:
<com.google.android.exoplayer2.ui.PlayerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/playerView"/>
then in fragment onCreateView I added these lines:
PlayerView playerView = v.findViewById(R.id.playerView);
playerView.getLayoutParams().width = ListPager.widthPixels;
playerView.getLayoutParams().height = ListPager.widthPixels;
if(player!=null)
player.release();
player = new SimpleExoPlayer.Builder(App.applicationContext).build();
playerView.setPlayer(player);
MediaItem mediaItem = MediaItem.fromUri(url);
player.setMediaItem(mediaItem);
player.prepare();
player.play();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | farhad.kargaran |