Browse Source

Added Recorder Class. Currently only Audio Recording working

bk_recording
Bastian Kohler 1 year ago
parent
commit
8c45cbeb56

+ 1
- 1
app/build.gradle View File

@@ -38,7 +38,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// Required for CameraX
def camerax_version = "1.2.2"
def camerax_version = "1.2.3"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"

+ 3
- 1
app/src/main/AndroidManifest.xml View File

@@ -3,7 +3,9 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:allowBackup="true"

+ 42
- 26
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -10,17 +10,21 @@ import androidx.camera.core.ExperimentalGetImage;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

@ExperimentalGetImage
public class MainActivity extends AppCompatActivity {

private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
private static final int PERMISSION_REQUEST_CODE = 111;


@Override
@@ -32,54 +36,66 @@ public class MainActivity extends AppCompatActivity {
PreviewView previewView = findViewById(R.id.previewView);


VideoDetector vd = new VideoDetector(this);
vd.setPreviewView(previewView);
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
@Override
public void onDetection(@NonNull DetectionReport detectionReport) {
detectionReport.log("OnDetection");
textView.setText(detectionReport.toString());
}
});
//vd.startDetection();
File directory = getFilesDir();


Recorder recorder = new Recorder(this);


ToggleButton toggleButton = findViewById(R.id.previewButton);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCameraAccess();
if (isCameraAccessAllowed() && toggleButton.isChecked())
getRecordVideoAccess();

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

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

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

}
else {
vd.stopDetection();
textView.setText("");
File[] files = directory.listFiles();
textView.setText(Arrays.toString(files));
recorder.stopRecording();
}
}
});
}

private boolean isCameraAccessAllowed() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
private boolean isRecordVideoAllowed() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
}

private void getCameraAccess() {
if (!isCameraAccessAllowed())
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
private void getRecordVideoAccess() {
if (!isRecordVideoAllowed()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
}
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {

boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraRights) {
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
boolean recordAudioRights = grantResults[1] == PackageManager.PERMISSION_GRANTED;

if (cameraRights && recordAudioRights) {
Toast.makeText(this, "permissions granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
Toast.makeText(this, "permissions denied", Toast.LENGTH_LONG).show();
}
}
}

+ 82
- 0
app/src/main/java/com/example/ueberwachungssystem/Recorder.java View File

@@ -0,0 +1,82 @@
package com.example.ueberwachungssystem;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;

import androidx.camera.core.Camera;

import java.io.File;
import java.io.IOException;

public class Recorder {

Context context;
MediaRecorder mediaRecorder = null;

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


public void startRecording() {

mediaRecorder = new MediaRecorder();

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.gpp");
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);



try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}

public void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
}
}



private void initMediaRecorder() {
// Set the audio and video source
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

// Set the output format and file path
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//String videoFilePath = getOutputMediaFilePath(); // Custom method to get the output file path
mediaRecorder.setOutputFile(context.getFilesDir());

// Set the video encoder and audio encoder
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

// Configure video size, frame rate, and other settings as needed
mediaRecorder.setVideoSize(640, 480); // Set the desired video size
mediaRecorder.setVideoFrameRate(30); // Set the desired frame rate
}


private String getOutputMediaFilePath() {
String videoFileName = "Video.mp4";
File storageDir = context.getFilesDir();

if (!storageDir.exists()) {
storageDir.mkdirs();
}

return storageDir.getAbsolutePath() + "/" + videoFileName;
}
}

Loading…
Cancel
Save