Added Recorder Class. Currently only Audio Recording working
This commit is contained in:
parent
19d1bfe1e3
commit
8c45cbeb56
@ -38,7 +38,7 @@ dependencies {
|
|||||||
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,7 +3,9 @@
|
|||||||
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"
|
||||||
|
@ -10,17 +10,21 @@ import androidx.camera.core.ExperimentalGetImage;
|
|||||||
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
|
||||||
@ -32,54 +36,66 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
PreviewView previewView = findViewById(R.id.previewView);
|
PreviewView previewView = findViewById(R.id.previewView);
|
||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
File directory = getFilesDir();
|
||||||
vd.setPreviewView(previewView);
|
|
||||||
vd.setOnDetectionListener(new VideoDetector.OnDetectionListener() {
|
|
||||||
@Override
|
Recorder recorder = new Recorder(this);
|
||||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
|
||||||
detectionReport.log("OnDetection");
|
|
||||||
textView.setText(detectionReport.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
//vd.startDetection();
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
getRecordVideoAccess();
|
||||||
if (isCameraAccessAllowed() && toggleButton.isChecked())
|
|
||||||
|
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();
|
File[] files = directory.listFiles();
|
||||||
textView.setText("");
|
textView.setText(Arrays.toString(files));
|
||||||
|
recorder.stopRecording();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isCameraAccessAllowed() {
|
private boolean isRecordVideoAllowed() {
|
||||||
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
|
return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
||||||
|
ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getCameraAccess() {
|
private void getRecordVideoAccess() {
|
||||||
if (!isCameraAccessAllowed())
|
if (!isRecordVideoAllowed()) {
|
||||||
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
|
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) {
|
boolean recordAudioRights = grantResults[1] == PackageManager.PERMISSION_GRANTED;
|
||||||
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user