In this video it shows the steps to create live streaming video from the phone’s camera on the Texture View widget of your Android App.
It uses the new CameraDevice.createCaptureSession(sessionConfiguration) API. The previous versions of this API are deprecated from API version 30 onwards.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.cameraapp;
import static android.Manifest.permission.CAMERA;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.params.OutputConfiguration;
import android.hardware.camera2.params.SessionConfiguration;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private TextureView textureView;
private CameraCaptureSession myCameraCaptureSession;
private String stringCameraID;
private CameraManager cameraManager;
private CameraDevice myCameraDevice;
private CaptureRequest.Builder captureRequestBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,
new String[]{CAMERA},
PackageManager.PERMISSION_GRANTED);
textureView = findViewById(R.id.textureView);
cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
startCamera();
}
private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
myCameraDevice = cameraDevice;
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
myCameraDevice.close();
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int i) {
myCameraDevice.close();
myCameraDevice = null;
}
};
private void startCamera() {
try {
stringCameraID = cameraManager.getCameraIdList()[1];
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
cameraManager.openCamera(stringCameraID, stateCallback, null);
} catch (CameraAccessException e) {
throw new RuntimeException(e);
}
}
public void buttonStartCamera(View view){
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
Surface surface = new Surface(surfaceTexture);
try {
captureRequestBuilder = myCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
OutputConfiguration outputConfiguration = new OutputConfiguration(surface);
SessionConfiguration sessionConfiguration = new SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
Collections.singletonList(outputConfiguration),
getMainExecutor(),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
myCameraCaptureSession = cameraCaptureSession;
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CameraMetadata.CONTROL_MODE_AUTO);
try {
myCameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
myCameraCaptureSession = null;
}
}
);
myCameraDevice.createCaptureSession(sessionConfiguration);
} catch (CameraAccessException e) {
throw new RuntimeException(e);
}
}
public void buttonStopCamera(View view){
try {
myCameraCaptureSession.abortCaptures();
} catch (CameraAccessException e) {
throw new RuntimeException(e);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextureView
android:id="@+id/textureView"
android:layout_width="316dp"
android:layout_height="330dp"
android:layout_marginStart="47dp"
android:layout_marginTop="38dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="136dp"
android:layout_marginTop="48dp"
android:onClick="buttonStartCamera"
android:text="Start Camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textureView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="136dp"
android:layout_marginTop="40dp"
android:onClick="buttonStopCamera"
android:text="Stop Camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots: