Rotate videoview while livestreaming - android

I have a videoview which play live streaming links fine in potrait mode with some channels listing below the videoview.
My problem is that i want to rotate my videoview to landscape mode (fullscreen and without showing the channel lists in the bottom) without buffering again or loading the stream again.
Thanks.

Add android:configChanges="orientation" to Activity tag on AndroidManifest.xml
it will stop the calling of onCreate() while rotation and prevent your video from rebuffering.
and use the following code in your activity.
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.d("tag", "Portrait");//Do your layout changes here
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.d("tag", "Landscape");//Do your layout changes here
}
}
Mark as up if it works for you.

Related

set screen orientation for Fragment Manager - Android

I want to rotate my app when the landscape mode is activated, and to be fixed in Portrait mode(like a normal app).
As I use a fragment manager, I decided to create an init method to programmatically decide to rotate or not my app instead of doing rotation for every fragments.
here's the method:
public class ActivityHelper {
public static void initialize(Activity activity) {
int orientation = activity.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
The problem is that the app always stays in Portrait mode.
Does someone have an idea? Thanks in advance

how to play video from same point in full landscape mode when videoview is in ListView android?

I am working on a app where I have list of videos shown in a Listview. And that Listview contains videoview to play video in the ListView itself.
(Note: I have restricted the screen mode to Portrait only)
When I try to play video in VideoView inside a ListView, it is working without any trouble. But what I actually want to achieve is, while the video is playing in videoView inside a Listview and if the user changes the screen mode to Landscape then video should be visible in fullscreen in landscape and start from the same point which it was while playing in Portrait mode.
Please help me out
Thanks
Use the ConfigurationChanged method of Activity.
You may put your VideoView in RelativeLayout
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
switchToFullScreen();
}
}
}
private void switchToFullScreen(){
RelativeLayout mContainer = (RelativeLayout) yourRelativeLayouyt;
mContainer.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
mContainer.setPadding(0, 0, 0, 0);
mContainer.requestFocus();
mContainer.bringToFront();
}

Video doesn't re-size when orientation changes

I have a video displayed using SurfaceView in a fragment. If I change the orientation of the screen while the video is playing it doesn't resize. For example, if the video is playing in landscape size (full screen), it doesn't go to portrait size when the screen is rotated. It remains if landscape size. If I start the video in landscape, it won't change to portrait.I am using the following in my manifest:
<activity
android:name=".BassActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
>
I have this in my "onCreate"
#Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setRetainInstance(true);
I would like for the video to re-size, for example from portrait to landscape when I rotate the screen, while it is playing.
I also added a toast message to onSurfaceChanged to see when it is triggered,but it is not trigger when I rotate the screen. How can I re-size the video after it has started playing?
use this config changes
if android:configchanges = "orientation"
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// for example the width of a layout
int width = 300;
int height = LayoutParams.WRAP_CONTENT;
WebView childLayout = (WebView) findViewById(R.id.webview);
childLayout.setLayoutParams(new LayoutParams(width, height));
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

How to detect the orientation mode while launching the app first

I am developing an app in which I need to provide different activity's background image on different orientation. So I've approached in this way:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
And
android:configChanges="locale|orientation|screenSize"
and in onCreate() I set the background image for portrait assuming user will launch the app staying in portrait mode.
Everything works fine as when users change their mode, the corresponding background is set and so on.
But if a user starts this app when the phone is in landscape mode, as It is shown the portrait image at the first launch as I assumed before user will launch app in portrait mode.
So how can I solve this problem? In one sentence, what is the best way to set different background image for different orientation ? am I in a right track?
In onCreate of your activity you can check for the current orientation using the this
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
In one sentence, what is the best way to set different background image for different orientation ?
Step #1: Delete everything you've done, most notably the whole android:configChanges stuff. Ignore the background image for now, and get the rest of your configuration change logic working.
Step #2: Create -land versions of the requisite resource directories, for whatever densities of this image that you have (e.g., res/drawable-land-hdpi/ to match your res/drawable-hdpi/)
Step #3: Move the landscape versions into the -land directories, naming them the same as their portrait equivalents (e.g., res/drawable-hdpi/background.png and res/drawable-land-hdpi/background.png)
Step #4: Just refer to common resource name in your android:background attribute (e.g., #drawable/background)
This way:
You stick to better configuration-change behavior, and
Android will give you the correct background at the correct time
You can check for orientation in onCreate
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180)
{
// Portrait
}
else
{
// landscape
}
check orientation changes after setContentView method
int orientation = getResources().getConfiguration().orientation;
if (orientation == 1){
utility.toast("portrait");
}else {
utility.toast("landscape");
}

Android determine screen orientation at runtime

Here's a pseudo code to detect screen rotate event, and decide to retain or changes the screen orientation.
public boolean onOrientationChanges(orientation) {
if(orientation == landscape)
if(settings.get("lock_orientation"))
return false; // Retain portrait mode
else
return true; // change to landscape mode
return true;
}
How do I make similar things in Android?
EDIT:
I'm actually looking answer on Where to handle orientation changes. I do not want to fix the orientation by adding screenOrientation="portrait".
I need something, similar to onConfigurationChanges(), where I can handle the orientation, but do no need me to manually redraw the view.
You need a Display instance firstly:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
Then orientation may be called like this:
int orientation = display.getOrientation();
Check orientation as your way and use this to change orientation:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I hope it helps.
Update:
Okay, let's say you've an oAllow var which is Boolean and default value is False.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
switch(orientation) {
case Configuration.ORIENTATION_PORTRAIT:
if(!oAllow) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
break;
case Configuration.ORIENTATION_LANDSCAPE:
if(!oAllow) {
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
}
You can add more choices.
I didn't try this sample, but at least tells you some clues about how to solve. Tell me if you got any error.
UPDATE
getOrientation() is already deprecated see here. Instead Use getRotation(). To check if the device is in landscape mode you can do something like this:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_90
|| orientation == Surface.ROTATION_270) {
// TODO: add logic for landscape mode here
}
Try running
getResources().getConfiguration().orientation
From your context object to figure out what is the screen orientation at runtime, the possible values are documented here
In order to catch the orientation change event you can find the answer in the Android Dev Guide: Handling the Configuration Change Yourself
From the guide :
For example, the following manifest code declares an activity that
handles both the screen orientation change and keyboard availability
change:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.
...
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
You don't need to intercept the event and then override it. Just use:
// Allow rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
// Lock rotation (to Landscape)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
Points to note here are, if on Jellybean and above this will allow a 180 degree rotation when locked. Also when unlocked this only allows rotation if the user's master settings is to allow rotation. You can forbid 180 degree rotations and override the master settings and allow rotation, and much much more, so check out the options in ActivityInfo
In addition, if you have pre-set that there is to be no rotation, then your activity will not be destroyed and then restarted, just for you to set the orientation back which will again cause the activity to be restarted; Thus setting what you want in advance can be much more efficient.
Pre Jellybean use ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -- no 180 degree rotation with this.
Check your android screen orientation at Runtime:
ListView listView = (ListView) findViewById(R.id.listView1);
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//do work for landscape screen mode.
listView.setPadding(0, 5, 0, 1);
} else if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//Do work for portrait screen mode.
listView.setPadding(1, 10, 1, 10);
}
Another solution to determine screen orientation:
public boolean isLandscape() {
return Resources.getSystem().getDisplayMetrics().widthPixels - Resources.getSystem().getDisplayMetrics().heightPixels > 0;
}

Categories

Resources