[英]How to set screen orientation according to a video's height and width
I am using videoview
, in the onPrepared
method and it returns the media player height and width.我在
onPrepared
方法中使用videoview
,它返回媒体播放器的高度和宽度。 Then I check if the width of the video is greater than the height and rotate the screen.然后我检查视频的宽度是否大于高度并旋转屏幕。 The problem is that if I rotate the screen orientation, it again calls the
onCreate
method and then recalls all of the code so it takes time to initialize the video again.问题是,如果我旋转屏幕方向,它会再次调用
onCreate
方法,然后调用所有代码,因此再次初始化视频需要时间。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.play_video_activity);
// Insert your Video URL
String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
// Find your VideoView in your video_main.xml layout
final VideoView videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
// Create a progressbar
final ProgressDialog pDialog;
pDialog = new ProgressDialog(PlayVideoActivity.this);
// Set progressbar title
pDialog.setTitle("Android Video Streaming Tutorial");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
PlayVideoActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
if(mp.getVideoWidth()> mp.getVideoHeight())
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
Add android:configChanges="orientation"
to your activity in manifest file在清单文件
android:configChanges="orientation"
到您的活动中
Declare variables in your activity在您的活动中声明变量
boolean orienChanged = false;
int lastOrientation = 0;
Override method覆盖方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if(orientation != lastOrientation){
orienChanged = true;
lastOrientation = orientation ;
}
}
Check the variable orienChanged
in onCreate()
for only initial time it will be false,otherwise it must be true.仅在初始时间检查
onCreate()
的变量orienChanged
它将为 false,否则它必须为 true。
Initialize new configuration oncreate... and make newConfigOD valiable global.在创建时初始化新配置...并使 newConfigOD 全局可用。
Configuration newConfigOD = getResources().getConfiguration();
Then override this method..然后覆盖这个方法..
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(videoview.getVideoWidth()> videoview.getVideoHeight()) {
if (newConfigOD.orientation != Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Try this... this should work as per your requirement.试试这个……这应该按照你的要求工作。 And remove this if condition from onCreate()
并从 onCreate() 中删除这个 if 条件
Auto set orientation according to video scale.根据视频比例自动设置方向。
Implement addVideoListener()实现 addVideoListener()
videoPlayer.addVideoListener(new VideoListener() { @Override public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { } @Override public void onRenderedFirstFrame() {} });
According to height & width set conditions根据高宽设置条件
if(width>height){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } if(height>width){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }
Your final objective would be like你的最终目标是
videoPlayer.addVideoListener(new VideoListener() { @Override public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) { if(width>height){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } if(height>width){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @Override public void onRenderedFirstFrame() {} });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.