如何继续播放视频,同时改变为横向模式的Android(How to keep playing vid

2019-06-25 11:53发布

我在玩视频(在VideoView在肖像模式)(全屏幕不是),当我改变

横向模式的视频站。

当我把它改为横向该视频将出现全屏幕上,并会继续打球。

什么建议吗?

Answer 1:

所有你需要做的是增加在AndroidManifest.xml活动:

 android:configChanges="orientation"

您的视频活动应该是这个样子。

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

如果你的目标API等级13以上,你必须包括screenSize ,除了价值orientation所描述的价值在这里 。 您的视频活动应该是这个样子。

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

然后将下面的方法添加到VideoPlayerActivity:

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


Answer 2:

横向全屏视频

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


Answer 3:

除非你指定,配置变化(如屏幕方向,语言,输入设备等的变化)将导致破坏你的当前活动,通过在onPause()的onStop()的正常活动的生命周期过程中去,的onDestroy()作为合适的。 如果活动已经在前台或对用户可见,一旦的onDestroy()被调用该实例,则活动的新实例将被创建,与任何savedInstanceState以前的实例已经从的onSaveInstanceState(束)产生。

到底发生了什么幕后:currnet VideoView活动(横向)被破坏,新的VideoView活动(纵向)已更改创建由于screenOrientation配置和immidiately destoryed(在这里你可以看到屏幕上的影响),在堆栈上一次活动被示出。

尝试处理这个方法,并检查

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

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

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


文章来源: How to keep playing video while changing to landscape mode android