我正在尝试将Exoplayer设置为Fragment,我在initializePlayer(String mediaUri)
中传递了视频网址
视频运行良好,但如果我旋转设备,视频重新启动,我会阅读几个教程来解决此问题但没有成功调用seekTo()
抱歉我的英文
public RecipeStepsVideoPlayerFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_recipe_steps_video_player, container, false);
ButterKnife.bind(this, view);
Bundle bundle=getArguments();
if(bundle!=null){
videoUrl=bundle.getString(VIDEO_URL_KEY);
}
initializePlayer(videoUrl);
stepDescription.setText(bundle.getString(STEP_DESCRIPTION_KEY));
return view;
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState != null) {
startAutoPlay = savedInstanceState.getBoolean(KEY_AUTO_PLAY);
startWindow = savedInstanceState.getInt(KEY_WINDOW);
startPosition = savedInstanceState.getLong(KEY_POSITION);
Toast.makeText(getContext(),startPosition.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
releasePlayer();
outState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
outState.putInt(KEY_WINDOW, startWindow);
outState.putLong(KEY_POSITION, startPosition);
}
public void initializePlayer(String mediaUri) {
if (mExoPlayer == null) {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
mPlayerView.setPlayer(mExoPlayer);
mediaDataSourceFactory = new DefaultDataSourceFactory(getContext(),
Util.getUserAgent(getContext(), "BakingX"),
(TransferListener<? super DataSource>) bandwidthMeter);
mediaSource = new ExtractorMediaSource
.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(mediaUri));
//my prolem I can not call seekTo
if (startPosition!=null) {
mExoPlayer.seekTo(startWindow,startPosition);
}
mExoPlayer.prepare(mediaSource);
mExoPlayer.setPlayWhenReady(playWhenReady);
}
}
private void releasePlayer() {
if (mExoPlayer!= null) {
startPosition = mExoPlayer.getCurrentPosition();
startWindow = mExoPlayer.getCurrentWindowIndex();
playWhenReady = mExoPlayer.getPlayWhenReady();
mExoPlayer.release();
mExoPlayer = null;
}
}
private void updateStartPosition() {
if (mExoPlayer != null) {
startAutoPlay = mExoPlayer.getPlayWhenReady();
startWindow = mExoPlayer.getCurrentWindowIndex();
startPosition = Math.max(0, mExoPlayer.getContentPosition());
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer(videoUrl);
}
}
@Override
public void onResume() {
super.onResume();
if (Util.SDK_INT <= 23 || mExoPlayer == null) {
initializePlayer(videoUrl);
}
}
@Override
public void onPause() {
super.onPause();
updateStartPosition();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
// releasePlayer();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
@Override
public void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
我的问题是我在移动广告投放后如何调用seekTo()
答案 0 :(得分:0)
一些问题:
1.删除playWhenReady
并将其更新到任何地方startAutoPlay
。
2.将代码从onViewRestored
移至onCreateView
。
3.设置媒体源后更新位置。
public RecipeStepsVideoPlayerFragment() {
// Required empty public constructor
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recipe_steps_video_player, container, false);
ButterKnife.bind(this, view);
Bundle bundle = getArguments();
if (bundle != null) {
videoUrl = bundle.getString(VIDEO_URL_KEY);
}
if (savedInstanceState != null) {
startAutoPlay = savedInstanceState.getBoolean(KEY_AUTO_PLAY);
startWindow = savedInstanceState.getInt(KEY_WINDOW);
startPosition = savedInstanceState.getLong(KEY_POSITION);
Toast.makeText(getContext(), startPosition.toString(), Toast.LENGTH_LONG).show();
}
initializePlayer(videoUrl);
stepDescription.setText(bundle.getString(STEP_DESCRIPTION_KEY));
return view;
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
releasePlayer();
outState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
outState.putInt(KEY_WINDOW, startWindow);
outState.putLong(KEY_POSITION, startPosition);
}
public void initializePlayer(String mediaUri) {
if (mExoPlayer == null) {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
mPlayerView.setPlayer(mExoPlayer);
mediaDataSourceFactory = new DefaultDataSourceFactory(getContext(),
Util.getUserAgent(getContext(), "BakingX"),
(TransferListener<? super DataSource>) bandwidthMeter);
mediaSource = new ExtractorMediaSource
.Factory(mediaDataSourceFactory)
.createMediaSource(Uri.parse(mediaUri));
mExoPlayer.prepare(mediaSource);
if (startPosition != null) {
mExoPlayer.seekTo(startWindow, startPosition);
}
mExoPlayer.setPlayWhenReady(startAutoPlay);
}
}
private void releasePlayer() {
if (mExoPlayer != null) {
startPosition = mExoPlayer.getCurrentPosition();
startWindow = mExoPlayer.getCurrentWindowIndex();
startAutoPlay = mExoPlayer.getPlayWhenReady();
mExoPlayer.release();
mExoPlayer = null;
}
}
private void updateStartPosition() {
if (mExoPlayer != null) {
startAutoPlay = mExoPlayer.getPlayWhenReady();
startWindow = mExoPlayer.getCurrentWindowIndex();
startPosition = Math.max(0, mExoPlayer.getContentPosition());
}
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer(videoUrl);
}
}
@Override
public void onResume() {
super.onResume();
if (Util.SDK_INT <= 23 || mExoPlayer == null) {
initializePlayer(videoUrl);
}
}
@Override
public void onPause() {
super.onPause();
updateStartPosition();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
// releasePlayer();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
@Override
public void onDestroy() {
super.onDestroy();
releasePlayer();
}