diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index cf0c3e0..e538369 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -11,10 +11,12 @@ import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Environment; +import android.view.SurfaceView; import android.view.View; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; +import android.widget.VideoView; import java.io.File; import java.io.IOException; @@ -33,13 +35,14 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); - PreviewView previewView = findViewById(R.id.previewView); - + SurfaceView surfaceView = findViewById(R.id.surfaceView); + VideoView videoView = findViewById(R.id.videoView); File directory = getFilesDir(); Recorder recorder = new Recorder(this); + recorder.setSurfaceView(surfaceView); ToggleButton toggleButton = findViewById(R.id.previewButton); @@ -64,8 +67,9 @@ public class MainActivity extends AppCompatActivity { } else { File[] files = directory.listFiles(); - textView.setText(Arrays.toString(files)); + //textView.setText(Arrays.toString(files)); recorder.stopRecording(); + recorder.playVideo(videoView); } } }); diff --git a/app/src/main/java/com/example/ueberwachungssystem/Recorder.java b/app/src/main/java/com/example/ueberwachungssystem/Recorder.java index 1524e46..ecd09a9 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Recorder.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Recorder.java @@ -1,82 +1,107 @@ 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.os.Environment; - -import androidx.camera.core.Camera; - -import java.io.File; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.widget.VideoView; import java.io.IOException; public class Recorder { - Context context; - MediaRecorder mediaRecorder = null; + private final Context context; + private Camera camera; + private MediaRecorder mediaRecorder = null; + private SurfaceView surfaceView; public Recorder (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.THREE_GPP); - mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.gpp"); - mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); - - - try { + // Open the camera + camera = Camera.open(); + camera.setPreviewDisplay(surfaceView.getHolder()); + camera.startPreview(); + + camera.unlock(); + + // Create a new MediaRecorder instance + mediaRecorder = new MediaRecorder(); + + // Set the camera as the video source + mediaRecorder.setCamera(camera); + + // Set the audio and video source + mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); + mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); + + + CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); + mediaRecorder.setProfile(camcorderProfile); + + // Set the output file path + mediaRecorder.setOutputFile(context.getFilesDir() + "/video.mp4"); + + mediaRecorder.prepare(); + mediaRecorder.start(); + } catch (IOException e) { e.printStackTrace(); } - mediaRecorder.start(); } public void stopRecording() { if (mediaRecorder != null) { - mediaRecorder.stop(); - mediaRecorder.reset(); - mediaRecorder.release(); - mediaRecorder = null; + try { + mediaRecorder.stop(); + mediaRecorder.reset(); + mediaRecorder.release(); + mediaRecorder = null; + } catch (RuntimeException e) { + // RuntimeException may be thrown if the MediaRecorder is in an invalid state + e.printStackTrace(); + } + + if (camera != null) { + try { + camera.reconnect(); + camera.stopPreview(); + camera.lock(); + camera.release(); + camera = null; + } catch (IOException e) { + e.printStackTrace(); + } + } } } - - - private void initMediaRecorder() { - // Set the audio and video source - mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); - mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); - - // Set the output format and file path - mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); - //String videoFilePath = getOutputMediaFilePath(); // Custom method to get the output file path - mediaRecorder.setOutputFile(context.getFilesDir()); - - // Set the video encoder and audio encoder - mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); - mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); - - // Configure video size, frame rate, and other settings as needed - mediaRecorder.setVideoSize(640, 480); // Set the desired video size - mediaRecorder.setVideoFrameRate(30); // Set the desired frame rate + public void setSurfaceView(SurfaceView surfaceView) { + this.surfaceView = surfaceView; } - private String getOutputMediaFilePath() { - String videoFileName = "Video.mp4"; - File storageDir = context.getFilesDir(); - - if (!storageDir.exists()) { - storageDir.mkdirs(); + private void playAudio() { + MediaPlayer mp = new MediaPlayer(); + try { + mp.setDataSource(context.getFilesDir() + "/audio.gpp"); + mp.prepare(); + mp.start(); + } catch (Exception e) { + e.printStackTrace(); } + } - return storageDir.getAbsolutePath() + "/" + videoFileName; + public void playVideo(VideoView videoView) { + videoView.setVideoPath(context.getFilesDir() + "/video.mp4"); + videoView.start(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 275c453..754daf2 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -27,11 +27,15 @@ android:layout_height="wrap_content" android:text="ToggleButton" /> - - + android:layout_height="400dp" /> + + + \ No newline at end of file