Browse Source

Added Audio Recorder. Its working

bk_video_test
Bastian Kohler 1 year ago
parent
commit
10e5cb3e61

+ 83
- 0
app/src/main/java/com/example/ueberwachungssystem/AudioRecorder.java View File

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

+ 4
- 1
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File







AudioRecorder audioRecorder = new AudioRecorder(this);


// video Stuff // video Stuff
VideoDetector vd = new VideoDetector(this); VideoDetector vd = new VideoDetector(this);
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
{ {
//vd.startDetection(); //vd.startDetection();
vd.startRecording(); vd.startRecording();
audioRecorder.startRecording();

} }
else { else {
//vd.stopDetection(); //vd.stopDetection();
vd.stopRecording(); vd.stopRecording();
audioRecorder.stopRecording();
} }
} }
}); });

Loading…
Cancel
Save