From a7a3152953e7f6ae21bb068d87b682bde6f7f409 Mon Sep 17 00:00:00 2001 From: Bastian Kohler Date: Sat, 17 Jun 2023 09:39:37 +0200 Subject: [PATCH] WIP: audio recorder --- .../ueberwachungssystem/AudioRecorder.java | 76 +++++++++++++++++++ .../ueberwachungssystem/MainActivity.java | 30 +++++--- app/src/main/res/layout/activity_main.xml | 6 ++ 3 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/example/ueberwachungssystem/AudioRecorder.java diff --git a/app/src/main/java/com/example/ueberwachungssystem/AudioRecorder.java b/app/src/main/java/com/example/ueberwachungssystem/AudioRecorder.java new file mode 100644 index 0000000..f90b744 --- /dev/null +++ b/app/src/main/java/com/example/ueberwachungssystem/AudioRecorder.java @@ -0,0 +1,76 @@ +package com.example.ueberwachungssystem; + +import android.content.Context; +import android.hardware.Camera; +import android.media.CamcorderProfile; +import android.media.MediaPlayer; +import android.media.MediaRecorder; +import android.util.Log; +import android.view.SurfaceView; +import android.widget.VideoView; + +import java.io.File; +import java.io.IOException; + +public class AudioRecorder { + private final Context context; + private Camera camera; + private MediaRecorder mediaRecorder = null; + private SurfaceView surfaceView; + + public AudioRecorder (Context context) { + this.context = context; + this.surfaceView = new SurfaceView(context); + } + + + public void startRecording() { + + mediaRecorder = new MediaRecorder(); + + mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); + mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); + mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); + mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.mp3"); + + + try { + mediaRecorder.prepare(); + mediaRecorder.start(); + } catch (IOException e) { + Log.e("audioRecorder", "prepare() failed"); + } + } + + public void stopRecording() { + if (mediaRecorder != null) { + try { + mediaRecorder.stop(); + mediaRecorder.release(); + mediaRecorder = null; + } catch (RuntimeException e) { + // RuntimeException may be thrown if the MediaRecorder is in an invalid state + e.printStackTrace(); + } + } + } + + public void playAudio() { + MediaPlayer mp = new MediaPlayer(); + try { + mp.setDataSource(context.getFilesDir() + "/audio.mp3"); + mp.prepare(); + mp.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public void deleteRecording(){ + File file = new File(context.getFilesDir() + "/audio.mp3"); + if (file.exists()) { + file.delete(); + } + } +} diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 25dffa9..dbce374 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -42,22 +42,15 @@ public class MainActivity extends AppCompatActivity { File directory = getFilesDir(); - VideoDetector vd = new VideoDetector(this); - vd.setOnDetectionListener(new Detector.OnDetectionListener() { - @Override - public void onDetection(@NonNull DetectionReport detectionReport) { - Log.d("onDetection", detectionReport.toString()); - } - }); - vd.startDetection(); - - + AudioRecorder ar = new AudioRecorder(this); //DetectionRecorder detectionRecorder = new DetectionRecorder(this); //detectionRecorder.setPreviewView(previewView); ToggleButton toggleButton = findViewById(R.id.previewButton); + ToggleButton toggleButton2 = findViewById(R.id.toggleButton2); + toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -67,18 +60,33 @@ public class MainActivity extends AppCompatActivity { if (isRecordVideoAllowed() && toggleButton.isChecked()) { + ar.startRecording(); //vd.startRecording(); - vd.playVideo(videoView); + //vd.playVideo(videoView); //detectionRecorder.playVideo(videoView); //detectionRecorder.start(); } else { + ar.stopRecording(); //vd.stopRecording(); //detectionRecorder.stop(); } } }); + + toggleButton2.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (toggleButton.isChecked()) + { + ar.playAudio(); + } + else { + ar.deleteRecording(); + } + } + }); } private boolean isRecordVideoAllowed() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2a66b2d..da7d34c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -28,6 +28,12 @@ android:text="ToggleButton" /> + +