Reputation: 63
I am creating a video streaming application , I want when I switch to landscape it goes to full screen but it should not load the video from the start again. I want to to switch smoothly without affecting the current playing.Help.
public class MainActivity extends Activity {
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.vid);
String vidAddress = "http://campusvibe.co.nf/blender.mp4";
Uri vidUri = Uri.parse(vidAddress);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(videoView);
videoView.setMediaController(vidControl);
videoView.setVideoURI(vidUri);
videoView.start();
}
}
This is for the portrait mode
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3090C7">
<VideoView
android:id="@+id/vid"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_gravity="center">
</VideoView>
</RelativeLayout>
This is for the landscape mode
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3090C7">
<VideoView
android:id="@+id/vid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"></VideoView>
</RelativeLayout>
Upvotes: 1
Views: 540
Reputation: 1298
By default, each time you change an orientation, the view is recreated again. to prevent this you can use the android:configChanges="orientation
but it is preferable to save the state as at when the orientation changes.
As your activity begins to stop, the system calls onSaveInstanceState()
so your activity can save state information with a collection of key-value pairs. The default implementation of this method saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView.
To save additional state information for your activity such as the position of the streaming video, you must implement onSaveInstanceState()
and add key-value pairs to the Bundle object. For example:
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the current streaming position or state
percentage = controls.getBufferPercentage()
position = controls.getCurrentPosition()
savedInstanceState.putInt("percentage", percentage);
savedInstanceState.putInt("position", position);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
implement onRestoreInstanceState
, this is called when re-creating.
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentPosition = savedInstanceState.getInt("position");
mCurrentPercentage = savedInstanceState.getInt("percentage");
// then you can use the MediaControls to restore the streaming app to the current position
}
Upvotes: 0
Reputation: 929
I believe you could use this:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt("movLength", mediaplayer.getCurrentPosition());
// value equals to where user watched the
// etc.
}
and on the activity check if the Bundle savedInstanceState != null:
int movlength = 0;
if (savedInstanceState != null){
movlength = savedInstanceState.getInt("movLength");
}
mediaplayer.seekto(movlength);
// etc
Copy above to the onCreate method
Upvotes: 0
Reputation: 5180
Check the IMA SDK sample app sources:
https://github.com/googleads/googleads-ima-android
It does what you want.
Hope this help you
Upvotes: 0
Reputation: 92
Add this line to your activity in which the video is being played in the AndroidManifest.xml file
android:configChanges="orientation" This prevents the activity from being recreated. Hope this helps!
Upvotes: 1