How to keep playing video while changing to landscape mode android

前端 未结 3 1129
野的像风
野的像风 2020-12-08 15:02

I\'m playing video (on VideoView) on portrait mode (not on full screen) and when I change to

landscape mode the video stop.

When I change it to

相关标签:
3条回答
  • 2020-12-08 15:31

    all you need to do is add this to activity in AndroidManifest.xml:

     android:configChanges="orientation"
    

    Your Video activity should look something like this.

     <activity android:name=".VideoPlayerActivity" 
      android:configChanges="orientation" />
    

    If your target API level 13 or higher, you must include the screenSize value in addition to the orientation value as described here. Your Video activity should look something like this.

     <activity android:name=".VideoPlayerActivity" 
      android:configChanges="orientation|screenSize" />
    

    And then add the following method to VideoPlayerActivity:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
    
    0 讨论(0)
  • 2020-12-08 15:35

    Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

    So what happened behind the scenes: currnet VideoView Activity (landscape) is destroyed, a new VideoView Activity (portrait) is created due screenOrientation configuration has been changed, and destoryed immidiately (where you can see the effects on screen), last activity in stack is shown.

    Try to handle this methods and check

        @Override
    protected void onResume() {
        mVideoView.resume();
        super.onResume();
    }
    
    @Override
    protected void onPause() {
        mVideoView.suspend();
        super.onPause();
    }
    
    @Override
    protected void onDestroy() {
        mVideoView.stopPlayback();
        super.onDestroy();
    }
    
    0 讨论(0)
  • 2020-12-08 15:39

    Full Screen Landscape Video

    AndroidManifext.xml (Setting the orientation)

            <activity
            android:name=".Video1"
            android:screenOrientation="landscape" />
    

    Video1.xml Code :

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Video1">
    
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
    </VideoView>
    

    Video1.java Code :

    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.WindowManager;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    public class Video1 extends AppCompatActivity {
    
    private VideoView videoView;
    private MediaController mediaController;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video1);
    
        videoView = findViewById(R.id.videoView);
        String fullScreen =  getIntent().getStringExtra("fullScreenInd");
        if("y".equals(fullScreen)){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getSupportActionBar().hide();
        }
    
        Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.YOUR_VIDEO_NAME);
    
        videoView.setVideoURI(videoUri);
    
        mediaController = new FullScreenMediaController(this);
        mediaController.setAnchorView(videoView);
    
        videoView.setMediaController(mediaController);
        videoView.start();
        }
    }
    

    FullScreenMediaControler.java Code :

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.FrameLayout;
    import android.widget.ImageButton;
    import android.widget.MediaController;
    
    public class FullScreenMediaController extends MediaController {
    
    private ImageButton fullScreen;
    private String isFullScreen;
    
    public FullScreenMediaController(Context context) {
        super(context);
    }
    
    @Override
    public void setAnchorView(View view) {
    
        super.setAnchorView(view);
    
        //image button for full screen to be added to media controller
        fullScreen = new ImageButton (super.getContext());
    
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.RIGHT;
        params.rightMargin = 80;
        addView(fullScreen, params);
    
        //fullscreen indicator from intent
        isFullScreen =  ((Activity)getContext()).getIntent().
                getStringExtra("fullScreenInd");
    
        if("y".equals(isFullScreen)){
            fullScreen.setImageResource(R.drawable.ic_fullscreen_exit);
        }else{
            fullScreen.setImageResource(R.drawable.ic_fullscreen);
        }
    
        //add listener to image button to handle full screen and exit full screen events
        fullScreen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
    
                Intent intent = new Intent(getContext(),Video1.class);
    
                if("y".equals(isFullScreen)){
                    intent.putExtra("fullScreenInd", "");
                }else{
                    intent.putExtra("fullScreenInd", "y");
                }
                ((Activity)getContext()).startActivity(intent);
            }
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题