Android video app surface texture listener

The android video app surface texture listener tutorial describes how setup a listener for the TextureView which get’s called once the TextureView is available.

And once the android TextureView is available we will be able to retrieve its width & height dimensions which are required to calculate the application’s preview and video resolutions.

Get Code

The code relating to this tutorial can be found on github

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-textureview

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-textureview

Steps

Remove the padding from the RelativeLayout

The intention here is to provide the full-screen dimensions to the TextureView

<RelativeLayout 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"
    tools:context="mobapptut.com.camera2videoimage.Camera2VideoImageActivity">

Add the TextureView to the RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="mobapptut.com.camera2videoimage.Camera2VideoImageActivity">

    <TextureView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textureView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/videoOnlineImageButton"
        android:contentDescription="@string/video_button"
        android:src="@mipmap/btn_video_online"
        android:layout_marginBottom="36dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="49dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cameraImageButton2"
        android:contentDescription="@string/camera_button"
        android:src="@mipmap/btn_camera"
        android:layout_alignTop="@+id/videoOnlineImageButton"
        android:layout_alignParentStart="true"
        android:layout_marginStart="57dp" />

</RelativeLayout>

 

Create TextureView & SurfaceTextureListener members

private TextureView mTextureView;
    private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    };

Initialise the TextureView member in the onCreate method

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2_video_image);

        mTextureView = (TextureView) findViewById(R.id.textureView);
    }

Add the onResume method

When the onResume method is called during application startup the TextureView needs to be checked to whether it is available.

If the TextureView is not available the surface texture will need to be setup with the surface texture listener member.

@Override
    public void onResume(){
        super.onResume();

        if(mTextureView.isAvailable()) {

        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

 

Android video app surface texture listener summary

In the android video app surface texture listener summary tutorial we were learnt

  • How to provide a listener for the TextureView so the application can be notified when the TextureView is available.
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>