我在videoview
方法中使用onPrepared
,它返回媒体播放器的高度和宽度。然后我检查视频的宽度是否大于高度并旋转屏幕。问题是,如果我旋转屏幕方向,它会再次调用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);
}
}
});
}
答案 0 :(得分:0)
将android:configChanges="orientation"
添加到清单文件
在您的活动中声明变量
boolean orienChanged = false;
int lastOrientation = 0;
覆盖方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if(orientation != lastOrientation){
orienChanged = true;
lastOrientation = orientation ;
}
}
检查orienChanged
中的变量onCreate()
是否只有初始时间为假,否则必须为真。
答案 1 :(得分:0)
初始化新配置oncreate ...并使newConfigOD成为可靠的全局。
Configuration newConfigOD = getResources().getConfiguration();
然后覆盖此方法..
@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);
}
}
试试这个...这应该按照你的要求工作。并从onCreate()
中删除此if条件答案 2 :(得分:0)
根据视频比例自动设置方向。
实施addVideoListener()
videoPlayer.addVideoListener(new VideoListener() {
@Override
public void onVideoSizeChanged(int width, int height, int
unappliedRotationDegrees, float pixelWidthHeightRatio) {
}
@Override
public void onRenderedFirstFrame() {}
});
根据高度和宽度设置条件
if(width>height){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
if(height>width){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
您的最终目标将是
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() {}
});