Android 视频播放器的方向改变
Android orientation change in video player
我有一个视频播放器。并希望同时启用横向和纵向。但问题是播放器每次在方向改变时都会下载文件。
我已经尝试使用 "onCofigurationChange",但它仍然无法正常工作。
我该如何解决这个问题?
P.S。我已经更改了 AndroidManifest.xml 文件并添加了
android:configChanges="orientation|screenSize|keyboardHidden"
所以问题出在我的 java 文件中。
这是我的代码:
public class VideoViewActivity extends Activity {
ProgressDialog pDialog;
VideoView videoview;
String VideoURL = "http://v.mover.uz/ep83Sfim_s.mp4";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_main);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
}
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setupActivity();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.videoview_main);
setupActivity();
}
public void setupActivity() {
videoview = (VideoView) findViewById(R.id.VideoView);
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.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();
}
});
}
}
在你的 onConfigurationChanged
中应该是:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//if you need change layout then do:
switch(newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(layout_landscape)
break;
case Configuration.ORIENTATION_PORTRAIT:
setContentView(layout_portrait);
break;
}
}
无需再次调用 setContentView(R.layout.videoview_main) and setupActivity()
。
并且还需要在您的 AdroidManifest 中添加 <activity android:name="<Your_video_avtivity>" android:configChanges="orientation" />
。
我有一个视频播放器。并希望同时启用横向和纵向。但问题是播放器每次在方向改变时都会下载文件。 我已经尝试使用 "onCofigurationChange",但它仍然无法正常工作。 我该如何解决这个问题?
P.S。我已经更改了 AndroidManifest.xml 文件并添加了
android:configChanges="orientation|screenSize|keyboardHidden"
所以问题出在我的 java 文件中。
这是我的代码:
public class VideoViewActivity extends Activity {
ProgressDialog pDialog;
VideoView videoview;
String VideoURL = "http://v.mover.uz/ep83Sfim_s.mp4";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview_main);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else
{
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
}
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setupActivity();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.videoview_main);
setupActivity();
}
public void setupActivity() {
videoview = (VideoView) findViewById(R.id.VideoView);
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.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();
}
});
}
}
在你的 onConfigurationChanged
中应该是:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//if you need change layout then do:
switch(newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
setContentView(layout_landscape)
break;
case Configuration.ORIENTATION_PORTRAIT:
setContentView(layout_portrait);
break;
}
}
无需再次调用 setContentView(R.layout.videoview_main) and setupActivity()
。
并且还需要在您的 AdroidManifest 中添加 <activity android:name="<Your_video_avtivity>" android:configChanges="orientation" />
。