Added Audio Recorder. Its working

This commit is contained in:
Bastian Kohler 2023-06-18 12:57:50 +02:00
parent f329fab62d
commit 10e5cb3e61
2 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,83 @@
package com.example.ueberwachungssystem;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class AudioRecorder {
private final Context context;
private MediaRecorder mediaRecorder = null;
private boolean isRecording = false;
private File outputDir; // Default: in app files directory
public AudioRecorder (Context context) {
this.context = context;
this.outputDir = context.getFilesDir();
}
public void startRecording() {
// Handle logic
if (outputDir == null)
return;
if (isRecording)
return;
isRecording = true;
// Setup Audio Recorder for output Format: 3GP
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(outputDir + "/" + generateFileName() + ".3gp");
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;
isRecording = false;
}
}
public boolean isRecording(){
return isRecording;
}
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
private String generateFileName(){
// Get the current timestamp
LocalDateTime currentTime = LocalDateTime.now();
// Define the format for the timestamp
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss");
// Return the timestamp as a string
return currentTime.format(formatter);
}
public void playAudio() {
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(context.getFilesDir() + "/audio.3gp");
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -41,6 +41,7 @@ public class MainActivity extends AppCompatActivity {
AudioRecorder audioRecorder = new AudioRecorder(this);
// video Stuff // video Stuff
VideoDetector vd = new VideoDetector(this); VideoDetector vd = new VideoDetector(this);
@ -55,7 +56,6 @@ public class MainActivity extends AppCompatActivity {
vd.startDetection(); vd.startDetection();
ToggleButton toggleButton = findViewById(R.id.toggleButton); ToggleButton toggleButton = findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener() { toggleButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -64,10 +64,13 @@ public class MainActivity extends AppCompatActivity {
{ {
//vd.startDetection(); //vd.startDetection();
vd.startRecording(); vd.startRecording();
audioRecorder.startRecording();
} }
else { else {
//vd.stopDetection(); //vd.stopDetection();
vd.stopRecording(); vd.stopRecording();
audioRecorder.stopRecording();
} }
} }
}); });