【问题标题】:How to keep playing video while changing to landscape mode android如何在更改为横向模式android时继续播放视频
【发布时间】:2012-06-17 09:06:52
【问题描述】:

我正在以纵向模式(不是全屏)播放视频(VideoView),当我更改为

横向模式视频停止。

当我将其更改为横向时,该视频将全屏显示并继续播放。

有什么建议吗?

【问题讨论】:

标签: java android orientation android-videoview


【解决方案1】:

除非您另外指定,否则配置更改(例如屏幕方向、语言、输入设备等的更改)将导致您当前的活动被破坏,并通过 onPause() 的正常活动生命周期过程,onStop( ) 和 onDestroy() 视情况而定。如果 Activity 位于前台或用户可见,则在该实例中调用 onDestroy() 后,将创建该 Activity 的新实例,使用前一个实例从 onSaveInstanceState(Bundle) 生成的 saveInstanceState。

那么幕后发生的事情: currnet VideoView Activity(横向)被销毁,由于 screenOrientation 配置已更改,创建了一个新的 VideoView Activity(纵向),并立即销毁(您可以在屏幕上看到效果),最后显示堆栈中的活动。

尝试处理这些方法并检查

    @Override
protected void onResume() {
    mVideoView.resume();
    super.onResume();
}

@Override
protected void onPause() {
    mVideoView.suspend();
    super.onPause();
}

@Override
protected void onDestroy() {
    mVideoView.stopPlayback();
    super.onDestroy();
}

【讨论】:

    【解决方案2】:

    您需要做的就是将其添加到 AndroidManifest.xml 中的活动中:

     android:configChanges="orientation"
    

    您的视频活动应如下所示。

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

    如果您的目标 API 级别为 13 或更高,则除了orientation 值之外,您还必须包含screenSize 值,如here 所述。您的视频活动应如下所示。

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

    然后在VideoPlayerActivity中添加如下方法:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
    

    【讨论】:

    • 我尝试了你的建议,但是当我以纵向模式播放时,与横向模式相比,视频应该是全屏的,但使用此代码时视频仍然很小
    • 太棒了,我一直在寻找它:P 终于
    • 这就是我要找的
    【解决方案3】:

    全屏横向视频

    AndroidManifext.xml(设置方向)

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

    Video1.xml 代码:

    <?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 代码:

    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 代码:

    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);
            }
        });
      }
    }
    

    【讨论】:

    • 全屏横向视频
    • Giving android:screenOrientation="landscape" 将仅修复 横向 模式下的活动(用户无法将其更改为纵向)。我相信您应该再次阅读问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多