WIP: audio recorder
This commit is contained in:
parent
0ad340bb32
commit
a7a3152953
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -42,22 +42,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
File directory = getFilesDir();
|
File directory = getFilesDir();
|
||||||
|
|
||||||
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
AudioRecorder ar = new AudioRecorder(this);
|
||||||
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
|
||||||
@Override
|
|
||||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
|
||||||
Log.d("onDetection", detectionReport.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
vd.startDetection();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//DetectionRecorder detectionRecorder = new DetectionRecorder(this);
|
//DetectionRecorder detectionRecorder = new DetectionRecorder(this);
|
||||||
//detectionRecorder.setPreviewView(previewView);
|
//detectionRecorder.setPreviewView(previewView);
|
||||||
|
|
||||||
ToggleButton toggleButton = findViewById(R.id.previewButton);
|
ToggleButton toggleButton = findViewById(R.id.previewButton);
|
||||||
|
ToggleButton toggleButton2 = findViewById(R.id.toggleButton2);
|
||||||
|
|
||||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
toggleButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@ -67,18 +60,33 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
if (isRecordVideoAllowed() && toggleButton.isChecked())
|
if (isRecordVideoAllowed() && toggleButton.isChecked())
|
||||||
{
|
{
|
||||||
|
ar.startRecording();
|
||||||
//vd.startRecording();
|
//vd.startRecording();
|
||||||
vd.playVideo(videoView);
|
//vd.playVideo(videoView);
|
||||||
//detectionRecorder.playVideo(videoView);
|
//detectionRecorder.playVideo(videoView);
|
||||||
//detectionRecorder.start();
|
//detectionRecorder.start();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
ar.stopRecording();
|
||||||
//vd.stopRecording();
|
//vd.stopRecording();
|
||||||
|
|
||||||
//detectionRecorder.stop();
|
//detectionRecorder.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
toggleButton2.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
if (toggleButton.isChecked())
|
||||||
|
{
|
||||||
|
ar.playAudio();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ar.deleteRecording();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRecordVideoAllowed() {
|
private boolean isRecordVideoAllowed() {
|
||||||
|
@ -28,6 +28,12 @@
|
|||||||
android:text="ToggleButton" />
|
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
|
<androidx.camera.view.PreviewView
|
||||||
android:id="@+id/previewView"
|
android:id="@+id/previewView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user