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

androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'


// Required for CameraX // 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-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}" implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}" implementation "androidx.camera:camera-lifecycle:${camerax_version}"

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

xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">


<uses-feature android:name="android.hardware.camera"/> <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 <application
android:allowBackup="true" android:allowBackup="true"

+ 42
- 26
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.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ToggleButton; 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 @ExperimentalGetImage
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {


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




@Override @Override
PreviewView previewView = findViewById(R.id.previewView); 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 toggleButton = findViewById(R.id.previewButton);
toggleButton.setOnClickListener(new View.OnClickListener() { toggleButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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 { 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 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, 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; 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 { } 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

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