java 如何在更改为横向模式时继续播放视频android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11031295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep playing video while changing to landscape mode android
提问by gilush14
I'm playing video (on VideoView
) on portrait mode (not on full screen) and when I change to
我正在VideoView
以纵向模式(不是全屏)播放视频(开),当我更改为
landscape mode the video stop.
横向模式视频停止。
When I change it to landscape that video will appear on full screen and will keep playing.
当我将其更改为横向时,该视频将全屏显示并继续播放。
Any Suggestion?
有什么建议吗?
回答by Vipul Shah
all you need to do is add this to activity in AndroidManifest.xml:
您需要做的就是将其添加到 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.
如果你的目标API等级13以上,你必须包括screenSize
除了价值orientation
所描述的价值在这里。您的视频活动应如下所示。
<activity android:name=".VideoPlayerActivity"
android:configChanges="orientation|screenSize" />
And then add the following method to VideoPlayerActivity:
然后将以下方法添加到 VideoPlayerActivity 中:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
回答by Mikhael Lauda
Full Screen Landscape Video
全屏风景视频
AndroidManifext.xml (Setting the orientation)
AndroidManifext.xml(设置方向)
<activity
android:name=".Video1"
android:screenOrientation="landscape" />
Video1.xml Code :
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 Code :
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 Code :
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);
}
});
}
}
回答by user1169079
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).
除非你另外指定,配置改变(比如屏幕方向、语言、输入设备等的改变)会导致你当前的Activity被销毁,经过onPause()、onStop()和onDestroy() 视情况而定。如果 Activity 已经在前台或对用户可见,一旦在该实例中调用 onDestroy(),则将创建该 Activity 的新实例,使用前一个实例从 onSaveInstanceState(Bundle) 生成的任何 savedInstanceState。
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.
那么幕后发生了什么:当前的 VideoView Activity(横向)被销毁,由于 screenOrientation 配置已更改而创建了一个新的 VideoView Activity(纵向),并立即销毁(您可以在屏幕上看到效果),堆栈中的最后一个 Activity显示。
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();
}