Browse Source

WIP: audio recorder

bk_recording
Bastian Kohler 1 year ago
parent
commit
a7a3152953

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

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

+ 19
- 11
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -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() {

+ 6
- 0
app/src/main/res/layout/activity_main.xml View File

@@ -28,6 +28,12 @@
android:text="ToggleButton" />


<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ToggleButton" />

<androidx.camera.view.PreviewView
android:id="@+id/previewView"
android:layout_width="match_parent"

Loading…
Cancel
Save