Browse Source

Added a second Recorder based on CameraX. Not working yet

bk_recording
Bastian Kohler 1 year ago
parent
commit
d37766ffaf

+ 8
- 16
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

import android.Manifest; import android.Manifest;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.VideoView; import android.widget.VideoView;


import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URISyntaxException;
import java.util.Arrays; import java.util.Arrays;


@ExperimentalGetImage @ExperimentalGetImage
TextView textView = findViewById(R.id.textView); TextView textView = findViewById(R.id.textView);
SurfaceView surfaceView = findViewById(R.id.surfaceView); SurfaceView surfaceView = findViewById(R.id.surfaceView);
VideoView videoView = findViewById(R.id.videoView); VideoView videoView = findViewById(R.id.videoView);
PreviewView previewView = findViewById(R.id.previewView);


File directory = getFilesDir(); File directory = getFilesDir();




Recorder recorder = new Recorder(this);
recorder.setSurfaceView(surfaceView);


RecorderX recorderX = new RecorderX(this);
//recorderX.setPreviewView(previewView);


ToggleButton toggleButton = findViewById(R.id.previewButton); ToggleButton toggleButton = findViewById(R.id.previewButton);
toggleButton.setOnClickListener(new View.OnClickListener() { toggleButton.setOnClickListener(new View.OnClickListener() {


if (isRecordVideoAllowed() && toggleButton.isChecked()) if (isRecordVideoAllowed() && toggleButton.isChecked())
{ {
//textView.setText(file.getAbsolutePath());
recorder.startRecording();

File file = new File(directory, "file.txt");

try { try {
file.createNewFile();
} catch (IOException e) {
recorderX.startRecording();
} catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

} }
else { else {
recorderX.stopRecording();
File[] files = directory.listFiles(); File[] files = directory.listFiles();
//textView.setText(Arrays.toString(files));
recorder.stopRecording();
recorder.playVideo(videoView);
textView.setText(Arrays.toString(files));
} }
} }
}); });

+ 152
- 0
app/src/main/java/com/example/ueberwachungssystem/RecorderX.java View File

package com.example.ueberwachungssystem;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.camera.core.Camera;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.Preview;
import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.video.FallbackStrategy;
import androidx.camera.video.MediaStoreOutputOptions;
import androidx.camera.video.Quality;
import androidx.camera.video.QualitySelector;
import androidx.camera.video.Recorder;
import androidx.camera.video.Recording;
import androidx.camera.video.VideoCapture;
import androidx.camera.video.VideoRecordEvent;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.core.util.Consumer;
import androidx.lifecycle.LifecycleOwner;

import com.google.common.util.concurrent.ListenableFuture;

import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;

public class RecorderX {

private final Context context;
private PreviewView previewView = null;
private ProcessCameraProvider cameraProvider;

private Recording recording = null;

public RecorderX(Context context) {
this.context = context;
}


@SuppressLint("MissingPermission")
public void startRecording() throws URISyntaxException {

final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
try {
cameraProvider = cameraProviderFuture.get();
} catch (ExecutionException | InterruptedException ignored) {
}


QualitySelector qualitySelector = QualitySelector
.from(Quality.HD, FallbackStrategy.higherQualityOrLowerThan(Quality.SD));


Recorder.Builder recorderBuilder = new Recorder.Builder();
recorderBuilder.setQualitySelector(qualitySelector);
Recorder recorder = recorderBuilder.build();

VideoCapture<Recorder> videoCapture = VideoCapture.withOutput(recorder);


// Create Preview
Preview preview = new Preview.Builder().build();
// Specify which Camera to use
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
// Update PreviewView if set
if (previewView != null)
preview.setSurfaceProvider(previewView.getSurfaceProvider());

try {
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, videoCapture, preview);
} catch (Exception e) {
Log.e("Error", "Use case binding failed", e);
}

String name = "recording.mp4";
Uri filesDirURI = Uri.fromFile(context.getFilesDir());
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, name);

MediaStoreOutputOptions mediaStoreOutput = new MediaStoreOutputOptions
.Builder(context.getContentResolver(), filesDirURI)
.setContentValues(contentValues)
.build();


Consumer<VideoRecordEvent> recordingListener = new Consumer<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent videoRecordEvent) {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
Toast.makeText(context, "Carture started", Toast.LENGTH_SHORT).show();
}
else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalize = (VideoRecordEvent.Finalize) videoRecordEvent;

if (!finalize.hasError()) {
Toast.makeText(context, "Carture succeeded", Toast.LENGTH_SHORT).show();
}
else {
recording.close();
recording = null;
Toast.makeText(context, "Carture failed", Toast.LENGTH_SHORT).show();
}
}
}
};


recording = videoCapture.getOutput()
.prepareRecording(context, mediaStoreOutput)
.withAudioEnabled()
.start(ContextCompat.getMainExecutor(context), recordingListener);

}




public void stopRecording() {
recording.stop();
}



/** Set PreviewView to show Image */
public void setPreviewView(PreviewView previewView) {
this.previewView = previewView;
}

private void playAudio() {
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(context.getFilesDir() + "/audio.gpp");
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}

public void playVideo(VideoView videoView) {
videoView.setVideoPath(context.getFilesDir() + "/video.mp4");
videoView.start();
}
}

+ 6
- 1
app/src/main/res/layout/activity_main.xml View File

<SurfaceView <SurfaceView
android:id="@+id/surfaceView" android:id="@+id/surfaceView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="400dp" />
android:layout_height="200dp" />

<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"
android:layout_height="200dp" />


<VideoView <VideoView
android:id="@+id/videoView" android:id="@+id/videoView"

Loading…
Cancel
Save