旋转到横向时的Exoplayer方向问题

问题描述 投票:1回答:2

当视频在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

android orientation exoplayer landscape-portrait savestate
2个回答
1
投票

尝试将此行添加到您的播放器清单文件中的活动声明:

android:configChanges="orientation|screenSize|layoutDirection"

0
投票

解:

在依赖项中添加以下代码:

// ViewModel
implementation "android.arch.lifecycle:extensions:1.1.1"

然后创建一个新类并将其扩展到ViewModel

public class Example extends ViewModel {
}

最后在你的片段onCreate()中:

ViewModelProviders.of(this).get(Example.class);

就是这样,你的轮换将被控制。

希望能帮助到你

© www.soinside.com 2019 - 2024. All rights reserved.