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

Posted

技术标签:

【中文标题】旋转到横向时的Exoplayer方向问题【英文标题】:Exoplayer Orientation problem when rotate to Landscape 【发布时间】:2019-02-07 13:31:38 【问题描述】:

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

【问题讨论】:

你做过什么样的测试?您是否知道在 onCreateView 方法中让您的代码段将其放回应该去的位置? 我正在尝试使用 Expresso 进行手动测试。我没有得到你的另一个问题。 @Kwright02 android 生命周期复杂。您是否 100% 确定每当屏幕旋转时它会再次调用 onCreateView? 是的,它再次调用,因为视频从头开始重新启动@Kwright02 好的,接下来要确定的是视频位置实际上正在被保存。一个好的方法可能是在它旋转屏幕之后,让它烘烤保持视频时间位置的变量的值,以确保它是正确的。如果这是正确的,那么还有其他一些事情需要考虑,但让我们先尝试一下。 【参考方案1】:

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

android:configChanges="orientation|screenSize|layoutDirection"

【讨论】:

以上是关于旋转到横向时的Exoplayer方向问题的主要内容,如果未能解决你的问题,请参考以下文章

从 ALAssset 检索时的 UIImage 方向问题

iOS App:窗口旋转总是纵向而不是横向

使用标签栏控制器时的方向问题

Android:编码的相机预览帧总是横向的。如何旋转?

Android模拟器不旋转到横向

Android模拟器不旋转到横向