Browse Source

Working video recorder based on deprecated camera api

bk_recording
Bastian Kohler 1 year ago
parent
commit
63db527f64

+ 7
- 3
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

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

+ 69
- 44
app/src/main/java/com/example/ueberwachungssystem/Recorder.java View File

@@ -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() {
try {
// Open the camera
camera = Camera.open();
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();

mediaRecorder = new MediaRecorder();
camera.unlock();

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.gpp");
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 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");


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

+ 8
- 4
app/src/main/res/layout/activity_main.xml View File

@@ -27,11 +27,15 @@
android:layout_height="wrap_content"
android:text="ToggleButton" />

<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="400dp" />

<androidx.camera.view.PreviewView
android:id="@+id/previewView"
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/black"/>
android:layout_height="wrap_content" />

</LinearLayout>

Loading…
Cancel
Save