如何使用 exoplayer 在 viewpager 中播放一个视频
Posted
技术标签:
【中文标题】如何使用 exoplayer 在 viewpager 中播放一个视频【英文标题】:how to play one video in view pager with exo player 【发布时间】:2020-10-05 22:58:25 【问题描述】:我正在制作一个应用程序,我在视图寻呼机中使用 exo 播放器播放视频,但问题是我一次无法播放一个视频。一次播放所有视频。我无法在滑动时暂停视频。请帮助。
**this is my activity where i am setting data in view pager**
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(),list);
viewpager.setAdapter(adapter);
这是我的视图寻呼机
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());
这是我获取视频网址并在 exo 播放器中播放的片段
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;
【问题讨论】:
【参考方案1】:int pager fragment layouts 我定义了这个:
<com.google.android.exoplayer2.ui.PlayerView
android:layout_
android:layout_
android:orientation="vertical"
android:id="@+id/playerView"/>
然后在片段 onCreateView 我添加了这些行:
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();
【讨论】:
【参考方案2】:我只想将任何未来的读者指向这个 repo,代码太棒了! https://github.com/nihk/video-pager
【讨论】:
虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review以上是关于如何使用 exoplayer 在 viewpager 中播放一个视频的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ExoPlayer 的 IMA 扩展在特定点展示广告?
如何使用 Exoplayer 播放从 Mediastore 获取的视频
ExoPlayer的PlayerView如何使用TextureView而不是SurfaceView?
如何在 android 中实现 Exoplayer 2.11.1?