在Exoplayer中播放视频时,效果很好,但在旋转时,视频又重新开始播放。 我想确保当视频旋转时,视频恢复到相同位置 我已经保存了玩家当前的位置,但是仍然无法正常工作。 请帮忙.....
public class RecipeStepDescriptionFragment extends Fragment {
@BindView(R.id.playerView)
PlayerView playerView;
@BindView(R.id.stepDescription)
TextView stepDescription;
@BindView(R.id.ingredientsCardSteps)
CardView ingredientsCardSteps;
@BindView(R.id.ingredientsListStepDescription)
TextView ingredientsListStepDescription;
@BindView(R.id.widgetButtonStepDescription)
FloatingActionButton widgetButton;
private SimpleExoPlayer player;
private static final String TAG = "StepDetail";
String videoUrl;
String longDescription;
boolean tablet;
String ingredients;
String name;
public RecipeStepDescriptionFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_recipe_step_description, container, false);
ButterKnife.bind(this, rootView);
if (getArguments() != null) {
videoUrl = getArguments().getString("videoURL");
longDescription = getArguments().getString("description");
tablet = getArguments().getBoolean("tablet");
ingredients = getArguments().getString("ingredients");
name = getArguments().getString("name");
//recipeList = (List<Steps>) getArguments().getSerializable("recipe_steps");*/
}
stepDescription.setText(longDescription);
if (!tablet) {
ingredientsCardSteps.setVisibility(View.GONE);
} else {
ingredientsListStepDescription.setText(ingredients);
}
if (videoUrl != null) {
playerView.setVisibility(View.VISIBLE);
if (videoUrl.equals("")) {
playerView.setVisibility(View.GONE);
} else {
player = ExoPlayerFactory.newSimpleInstance(getContext(), new DefaultTrackSelector());
playerView.setPlayer(player);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "exo-demo"));
ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoUrl));
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}
} else {
playerView.setVisibility(View.GONE);
}
if (savedInstanceState != null && player != null) {
player.seekTo(savedInstanceState.getLong("current_position"));
player.setPlayWhenReady(savedInstanceState.getBoolean("play_state"));
}
widgetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getActivity().getSharedPreferences("INGREDIENTS", MODE_PRIVATE).edit();
editor.putString("ingredients", ingredients);
editor.putString("name", name);
editor.commit();
Toast.makeText(getContext(), "Widget Added to Home Screen", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
public void initializePlayer(){
player = ExoPlayerFactory.newSimpleInstance(getContext(), new DefaultTrackSelector());
playerView.setPlayer(player);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "exo-demo"));
ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoUrl));
player.prepare(mediaSource);
player.setPlayWhenReady(true);
}
@Override
public void onStart() {
super.onStart();
if ((Util.SDK_INT > 23)) {
initializePlayer();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (player != null) {
outState.putLong("current_position", player.getCurrentPosition());
outState.putBoolean("play_state", player.getPlayWhenReady());
}
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "onStop:called ");
playerView.setPlayer(null);
if (player != null)
player.release();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy:called ");
playerView.setPlayer(null);
}
@Override
public void onResume() {
super.onResume();
if ((Util.SDK_INT <= 23 || player == null)) {
initializePlayer();
}
}
}
这是到存储库的链接。 https://github.com/Rahulxx01/Baking-App-Nanodegree
答案 0 :(得分:1)
尝试将此行添加到清单文件中的玩家活动声明中:
android:configChanges="orientation|screenSize|layoutDirection"
答案 1 :(得分:0)
解决方案:
在依赖项中添加以下代码:
// ViewModel
implementation "android.arch.lifecycle:extensions:1.1.1"
然后创建一个新类并将其扩展到ViewModel
public class Example extends ViewModel {
}
然后最后在Fragment的onCreate()中:
ViewModelProviders.of(this).get(Example.class);
就是这样,您的旋转将受到控制。
希望有帮助