Android videoview doesn't play in landscape mode - android

I have an activity that play a video with VideoView. Video starting in onCreate, and starts only if layout is in portrait mode, after it has started i can change to landscape and video continue to playing correctly.
The problem is when starting activity in landscape mode, in this case video doesn't play.
AndroidManifest.xml
<activity
android:name="...."
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_fullscreen_rec"
android:launchMode="singleTop"
android:parentActivityName="...." >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="...." />
</activity>
In onCreate method I simply set videosource and start videoview.
Then I have:
#Override
#SuppressLint("NewApi")
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

Add this code after the onCreate Method in your activity.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
please check this. tell me if this works ?

Related

Youtube Player has been released when activity created on device rotate

YouTube player has been released when devices rotate and activity recreated. Then play video. Shows YouTube player has released. I don't know where it released. I have already set it to Null onDestroy Method.
`mPlayer.loadVideo(videoId);`
//pointing YouTube Player released
My solution is to save the current playing time by YouTubePlayer getCurrentTimeMillis() in onSaveInstanceState(Bundle state) when screen rotation gets triggered, and get the time back from onRestoreInstanceState(Bundle state).
Sample code would be:
static final String CURRENT_PLAY_TIME = "current_play_time";
int current_time;
#Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putInt(CURRENT_PLAY_TIME, youTubePlayer.getCurrentTimeMillis());
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
current_time = state.getInt(CURRENT_PLAY_TIME);
}
And after restoring the current playing time let YouTubePlayer load video with it.
youTubePlayer.loadVideo(VIDEO_ID, current_time);
Hope it helps!
A posible solution is make your app vertical or horizontal permanent
For doing this write within your manifest:
<android:screenOrientation="portrait"> //vertical
<android:screenOrientation="landscape"> //horizontal
I have found my Excelent solution..all going good with youtube player
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (mPlayer != null)
//Set YouTube Player here;
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mPlayer != null)
//Set YouTube Player here;
}
}

Pressing the power button to turn off screen is restarting the activity

Here is what is happening. I have a simple camera application that takes a picture. I mostly copied the code from the Android Camera javadoc
1) I press the powerbutton. I see the following in my logs.
I/ImageUploader(15724): OnPause
I/ImageUploader(15724): Surface Preview Destroyed
I/ImageUploader(15724): OnCreate
I/ImageUploader(15724): OnResume
I/ImageUploader(15724): OnPause
What i down understand is why is OnCreate being called after onPause?
Here is my OnPause code.
#Override
protected void onPause() {
super.onPause();
Logger.i("OnPause");
//turn off the screen
if(previewLayout!=null){
previewLayout.setKeepScreenOn(false);
}
//release the camera
if(camera != null) {
camera.stopPreview();
//preview.setCamera(null);
camera.release();
camera = null;
//preview.mHolder.addCallback(null);
preview = null;
}
}
It was simple. It was occurring because of configuration change. Just add it to your activity in your manifest.
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
android:name=".About"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>

Android: Pausing and restart audio on new Activity, but pause and resume on screen rotate

I have an android app where in my Main activity I can play music onCreate(), pause music onPause() and restart music onResume(). I am using MediaPlayer. The problem is that I don't want the music to restart onResume() when I'm rotating the screen on my Main activity. I only want the music to restart when I'm coming back to my Main activity from another activity. Any suggestions?
private MediaPlayer mp;
mp = MediaPlayer.create(MainActivity.this, R.raw.always_sunny);
mp.setLooping(true);
mp.start();
#Override
protected void onPause() {
super.onPause();
mp.getCurrentPosition();
mp.pause();
}
#Override
protected void onResume() {
super.onResume();
mp.seekTo(0);
mp.start();
}
Add the following attribute to the activity in the manifest:
android:configChanges="keyboardHidden|orientation|screenSize"
This would avoid that the activity restarts on orientation changes or when the keyboard appears.
If you need to do something when the rotation changes (for example, change an image in your case), add the following method to your activity:
#Override
public void onConfigurationChanged (Configuration newConfig) {
// Change here your image
}

Disable Orientation Change while recording Video

Does anyone know how to disable Orientation change while recording ?
I tried something like this, but no success. any ideas?
#Override
public void onConfigurationChanged(Configuration newConfig) {
if (!recordingAudio && !recordingVideo) {
super.onConfigurationChanged(newConfig);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I've done it by adding the following in the manifest file as an attribute of the activity tag:
android:screenOrientation="nosensor"

Problems with Acitivity LifeCycle with VideoView playback

Hi all i've ran into another problems with VideoView.
Then video is playing, and I put device asleep, using hard button, onPause() is called. But it followed by:
03-17 11:26:33.779: WARN/ActivityManager(884): Activity pause timeout for HistoryRecord{4359f620 com.package/com.package.VideoViewActivity}
And then i have onStart()/onResume() again and Video starts playing. I've try to move code around onStart()/onStop() - doesn't seems to make difference.
sample code :
public class VideoViewActivity extends Activity {
private String path = "";
private VideoView mVideoView;
private static final String MEDIA_URL = "media_url";
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView)findViewById(R.id.surface_view);
path = getIntent().getStringExtra(MEDIA_URL);
}
#Override
public void onResume() {
super.onResume();
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
}
#Override
public void onPause() {
super.onPause();
mVideoView.stopPlayback();
mVideoView.setMediaController(null);
}
}
Why is it happening? And how do I stop that?
It's not a greatest experience than you put your device to sleep and it starts playing video
OK, Looks like the behavior is related to activity lifecycle and the fact that VideoViewActivity is set to landscape in the manifest. Adding
android:configChanges="keyboardHidden|orientation"
for that activity
seems to fix the problem and then you put device to sleep only onPause() called vs before - all lifecycle methods were executed.
I'll do more testing to make sure it fixed...

Categories

Resources