Exoplayer-在片段内旋转时保存和还原状态

时间:2018-08-18 12:30:32

标签: java android exoplayer

我有一个托管SimpleExoplayer的片段。我想确保我能正确处理屏幕旋转。现在,播放器将重置为屏幕旋转的开始。我已经在onStart()和onResume()中实现了方法,所以我很好奇我还需要什么其他代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_recipe_details_three, container,false);

    ............

    mStepDescription.setText(step.getDescription());
    mVideoURL = step.getVideoURL();
    mSimpleExoPlayer = v.findViewById(R.id.exoplayer);
    mExoPlayerPlaceholder = v.findViewById(R.id.exoplayer_placeholder);
    if (mVideoURL == null || mVideoURL.isEmpty()){
        mSimpleExoPlayer.setVisibility(View.GONE);
        mExoPlayerPlaceholder.setVisibility(View.VISIBLE);
    }


    return v;
}

onStart:

    @Override
public void onStart() {
    super.onStart();
    initializePlayer();
}

onPause:

    @Override
public void onPause() {
    super.onPause();
    if (mExoPlayer!=null) {
        mExoPlayer.release();
        mExoPlayer = null;
    }
}

初始化:

private void initializePlayer(){
    // Create a default TrackSelector
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
            new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector =
            new DefaultTrackSelector(videoTrackSelectionFactory);

    //Initialize the player
    mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
    mSimpleExoPlayer.setPlayer(mExoPlayer);


    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory =
            new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "CloudinaryExoplayer"));

    // Produces Extractor instances for parsing the media data.
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();



    // This is the MediaSource representing the media to be played.
    Uri videoUri = Uri.parse(mVideoURL);
    MediaSource videoSource = new ExtractorMediaSource(videoUri,
            dataSourceFactory, extractorsFactory, null, null);

    // Prepare the player with the source.
    mExoPlayer.prepare(videoSource);
}

4 个答案:

答案 0 :(得分:2)

您不需要为此更改清单。以下代码应该可以工作

override fun onSaveInstanceState(outState: Bundle) {
    outState.putLong(KEY_PLAYER_POSITION, exoPlayer.contentPosition)
    outState.putBoolean(KEY_PLAYER_PLAY_WHEN_READY, exoPlayer.playWhenReady)
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
    super.onViewStateRestored(savedInstanceState)
    savedInstanceState?.let {
        exoPlayer.seekTo(it.getLong(KEY_PLAYER_POSITION))
        exoPlayer.playWhenReady = it.getBoolean(KEY_PLAYER_PLAY_WHEN_READY)
    }
}

答案 1 :(得分:1)

我使用屏幕方向和Exoplayer进行游戏,以正确处理它,而不会入侵或绕开它。据我记得,每次我尝试尝试时都会遇到障碍。

我想最后一个是视图需要几毫秒才能再次显示视频,播放器也要跟上播放的速度(有趣的是我的播放器是静态的,并且使用了应用程序上下文进行初始化)。

最后,我最终添加了orientation标志以进行显示。

现在旋转屏幕时非常流畅

答案 2 :(得分:0)

我认为您可以选择禁用清单上的配置更改(应谨慎处理)。

您要在此处尝试实现的另一种选择是保存视频的位置,并在重新创建片段时寻找它。 为此,您需要:

1)将视频播放器的位置和播放状态保存在onSaveInstanceState中:


    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putLong(PLAYER_CURRENT_POS_KEY, Math.max(0, mPlayer.getCurrentPosition()));
        outState.putBoolean(PLAYER_IS_READY_KEY, mPlayer.getPlayWhenReady());
    }

2)重新创建片段后,在您的intitalizePlayer()方法中,检查保存的状态并从中恢复播放:


    private boolean resumePlaybackFromStateBundle(@Nullable Bundle inState) {
        if (inState != null) {
            mPlayer.setPlayWhenReady(inState.getBoolean(PLAYER_IS_READY_KEY));
            mPlayer.seekTo(inState.getLong(PLAYER_CURRENT_POS_KEY));
            return true;
        }
        return false;
    }

希望对您有帮助。

答案 3 :(得分:0)

在清单中将configChanges添加到您的活动中,如下所示:

<activity android:name="com.google.android.exoplayer.demo.PlayerActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
android:launchMode="singleTop"
android:label="@string/application_name"
android:theme="@style/PlayerTheme">