|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
} |
|
|
|
|
|
} |