I have an activity which should display a VideoView in landscape, but the activity itself must not be in landscape mode, so this is what i have.
<activity
android:name=".gui.VideoPlayer"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
</activity>
<?xml version="1.0" encoding="utf-8"?>
Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:id="#+id/myvideoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
(The activity itself must remain in portrait mode because the device will be put in a case which covers the navigation and statusbars, but in landscape mode it wont cover them any more)
I know this is not the optimal answer but I had the same issue a while ago and found a pretty convenient workaround that might help you too.
Try to use a TextureView instead of VideoView because when you rotate it, the content inside rotates as well.
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_texture_view"
android:rotation="90"
android:scaleX="1.8"
/>
Next you'll have to create a SurfaceTextureListener and set it to your view like below:
TextureView.SurfaceTextureListener mTextureListener = new TextureView.SurfaceTextureListener() {
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Surface s = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(currentPageVideoUrl);
mMediaPlayer.setSurface(s);
mMediaPlayer.prepare();
}catch (Exception e){e.printStackTrace();}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
And now just set the listener to your TextureView:
mTextureView.setSurfaceTextureListener(mTextureListener);
All that's left to do is to call this to start the video:
mMediaPlayer.start();
BONUS:If you want to make more complex adjustments like adjusting the video aspect ratio You can check this library for more details.
Related
I am playing video in Android Video view.Issue is video is not resizing as per device ratio. On big devices it stretched or in small device it is skeqzes. Is there any way to maintain ratio and fill full width like center crop.
I have checked following answers:
Resize video to fit the VideoView
Android VideoView orientation change with buffered video
But nothing works.
My code :
mBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
mBinding.placeholder.setVisibility(View.GONE);
return true;
}
return false;
}
});
mp.setLooping(true);
}
});
XML :
<VideoView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</VideoView>
Wrap the whole xml layout into a RelativeLayout. then modify your VideoView tag as following-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/videoView"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
What I want is when I scroll up the scrollview the gallery should be open and when I scroll down then the surfaceview(camera) should be visible like OLX. I have explained my requirement in the image.hope I explained my question in proper manner.
I have explained my requirement in this image
im also posting my xml codes to let you properly understand.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ali.ansofexperts.Question_Photo">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/camera_preview">
</FrameLayout>
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView"
android:fillViewport="true"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:minHeight="400dp"
android:layout_height="wrap_content"
android:id="#+id/click"
android:orientation="horizontal">
</LinearLayout>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:columnWidth="90dp"
android:numColumns="auto_fit" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
android:gravity="center" android:layout_gravity="bottom"
android:layout_marginTop="400dp"></GridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I am posting this Reference for beginner
https://android-arsenal.com/details/1/5987
This Reference have the complete solution about my question,hope this will helpful.
Try this
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
Refer this document you can achieve surfaceview with camer...gallery
party you can try your self..
https://developer.android.com/guide/topics/media/camera.html
https://github.com/googlesamples/android-Camera2Video
Activity supports Landscape mode
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="landscape"
android:label="#string/app_name" >
where I have allocated 50% space to Video Player (using FrameLayout) and rest 50% to ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/video_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ListView
android:id="#+id/video_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Now, I have started playing video > moved to full screen mode > pressed back to exit full screen mode > getting Activity in Portrait mode (whereas I was expecting to get it in Landscape mode)
boolean isFullScreen = false;
#Override
public void onGoToFullscreen() {
isFullScreen = true;
videoListView.setVisibility(View.INVISIBLE);
}
#Override
public void onReturnFromFullscreen() {
videoListView.setVisibility(View.VISIBLE);
}
#Override
public void onBackPressed() {
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
}
else {
super.onBackPressed();
}
}
/* I am not sure,can you put this line of code in on back pressed method ? */
Log.d("boolean:-", Boolean.toString(isFullScreen));
if(isFullScreen) {
imaPlayer.getContentPlayer().setFullscreen(false);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
super.onBackPressed();
}
I'm trying and failing at properly showing fullscreen video.
My WebChromeClient implements onHideCustomView and onShowCustomView. My layout has separate layouts for showing video and displaying webView. The problem is that sometimes it works correctly, and other times it does not. It happens only with a single website. I've noticed, that video goes fullscreen correctly on JW player 7.2.2, while it fails often on JW player 6.12.4945.
Here's how it looks when clicking fullscreen button or double clicking the video. Notice that there is website contents above the video, and there is empty view at the bottom of screen: https://drive.google.com/file/d/0B942jMuE3MylMVZQNVp6X0Y2ZGc/view?usp=sharing
webChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
#Override
public void onHideCustomView() {
fullscreen = false;
showUI();
mVideoView.removeAllViews();
}
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
fullscreen = true; //for onBackPressed
hideUI();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mVideoView.addView(view, params);
}
};
<RelativeLayout
android:id="#+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Let me begin with the coding first.
My xml file (relevant part of it) contains this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff">
<SurfaceView
android:id="#+id/cameraView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layStatus"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:id="#+id/txtStatus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#90000000"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:gravity="center_vertical|center_horizontal"
android:textSize="20dip" />
</LinearLayout>
</FrameLayout>
As you can see I've got a basic set up, a frame layout which contains a surfaceview (which has visibility set to gone) and another layout with a simple TextView.
Here is what I have in my activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
cameraView = (SurfaceView)findViewById(R.id.cameraView);
surfaceHolder = cameraView.getHolder();
surfaceHolder.addCallback(CompassActivity.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void btnCameraOnClick(View target) {
cameraView.setVisibility(View.VISIBLE);
}
Starting the camera preview is done in Surface changed, which fires when visibility of the surface changes.
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
txtStatus.setVisibility(View.Visible);
txtStatus.setText("Starting camera");
camera = Camera.open();
if (camera != null){
try {
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size size = sizes.get(0);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();
txtStatis.setVisibility(View.GONE);
} catch (IOException e) {
}
}
}
So what's the problem ? I want to simply display a text while the camera preview starts. To do this, I make the txtStatus visible and set a text to it. When the preview is started, I simply hide it. Well, it doesn't work like this, When I press a button to start camera preview, the UI thread freezes waiting for the preview and my status message is not displayed. How can I fix it ? There is a solution at HERE but I was thinking that maybe there is a simpler one.
Thank you for your time.
Camera start up takes some time - external process shall be started and everything set up. I would speculate, that surface changed callback is executed directly from setVisibility() and blocks UI thread. Solution is pretty simple - just spawn new thread and do camera init there.
In my OCR applicatios I start camera in onResume() - since I always need working preview
You may find my OCR android demos helpful:
http://sourceforge.net/projects/javaocr/
( camera preview, with overlays )