在我的应用程序中,视频播放期间的UI取决于设备方向。当设备处于纵向模式时,视频会在嵌入RecyclerView.ViewHolder
RecyclerView
中的Fragment
对象中播放,ViewPager
位于Fragment
内RecyclerView
内DialogFragment
。当设备更改为横向模式时,RecyclerView
中的视频停止付费,弹出RecyclerView.ViewHolder
,并且应该开始播放@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
...
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
orientationControlViewModel.getOrientationLiveData().setValue(true);
...
}
}
中的视频停止播放的位置。当屏幕旋转发生时,活动通过以下代码通知LiveData
:
RecyclerView.ViewHolder
DialogFragment
正在观察LiveData
个对象。当OrientationLiveData的值设置为true时,视频暂停,网址和当前时间通过另一个holder.orientationControlViewModel.getOrientationLiveData().observe(holder, orientationChanged -> {
//currently doesn't get called
Log.i("orientation3", "viewholder observing orientation live data");
if (orientationChanged && holder.equals(getNowPlayingViewHolder())){
//todo there is a lag after rotation, show loading spinner
holder.binding.videoLayout.postVideoView.getPlayer().setPlayWhenReady(false);
VideoInfo videoInfo = new VideoInfo(joke.getMediaURL(),
holder.binding.videoLayout.postVideoView.getPlayer().getCurrentPosition());
holder.orientationControlViewModel.getVideoLiveData().setValue(videoInfo);
} ...
});
发送到DialogFragment
,如下所示:
public void onResume() {
super.onResume();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getActivity(),
Util.getUserAgent(getActivity(), "Hilarity"), bandwidthMeter);
SimpleCache cache = new SimpleCache(getActivity().getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024^2*100));
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(cache, dataSourceFactory);
// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
if (simpleExoPlayerView.getPlayer() != null) {
simpleExoPlayerView.getPlayer().prepare(new ExtractorMediaSource(Uri.parse(url),
dataSourceFactory, extractorsFactory, null, null), false, true);
simpleExoPlayerView.getPlayer().seekTo(position);
Log.i("orientation4", "position = " + position + " in onResume");
simpleExoPlayerView.getPlayer().setPlayWhenReady(true);
}
}
记录显示DialogFragment
已收到要开始的位置。在onResume中,调用了seekTo(代码如下所示),但视频从头开始。
http://localhost:3000/base/a
http://localhost:3000/base/b
我的问题是,如何让base
中的视频从seekTo()方法中用作参数的位置开始?如果您对可能比当前实现的架构更好的架构有建议,请随时留下输入。