Working video recorder based on deprecated camera api

This commit is contained in:
Bastian Kohler 2023-05-31 10:31:02 +02:00
parent 8c45cbeb56
commit 63db527f64
3 changed files with 89 additions and 56 deletions

View File

@ -11,10 +11,12 @@ import android.Manifest;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ToggleButton; import android.widget.ToggleButton;
import android.widget.VideoView;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -33,13 +35,14 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView); 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(); File directory = getFilesDir();
Recorder recorder = new Recorder(this); Recorder recorder = new Recorder(this);
recorder.setSurfaceView(surfaceView);
ToggleButton toggleButton = findViewById(R.id.previewButton); ToggleButton toggleButton = findViewById(R.id.previewButton);
@ -64,8 +67,9 @@ public class MainActivity extends AppCompatActivity {
} }
else { else {
File[] files = directory.listFiles(); File[] files = directory.listFiles();
textView.setText(Arrays.toString(files)); //textView.setText(Arrays.toString(files));
recorder.stopRecording(); recorder.stopRecording();
recorder.playVideo(videoView);
} }
} }
}); });

View File

@ -1,82 +1,107 @@
package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;
import android.content.Context; import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaPlayer;
import android.media.MediaRecorder; import android.media.MediaRecorder;
import android.os.Environment; import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.camera.core.Camera; import android.widget.VideoView;
import java.io.File;
import java.io.IOException; import java.io.IOException;
public class Recorder { public class Recorder {
Context context; private final Context context;
MediaRecorder mediaRecorder = null; private Camera camera;
private MediaRecorder mediaRecorder = null;
private SurfaceView surfaceView;
public Recorder (Context context) { public Recorder (Context context) {
this.context = context; this.context = context;
this.surfaceView = new SurfaceView(context);
} }
public void startRecording() { public void startRecording() {
try {
// Open the camera
camera = Camera.open();
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
camera.unlock();
// Create a new MediaRecorder instance
mediaRecorder = new MediaRecorder(); mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // Set the camera as the video source
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setCamera(camera);
mediaRecorder.setOutputFile(context.getFilesDir() + "/audio.gpp");
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 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.prepare();
mediaRecorder.start();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
mediaRecorder.start();
} }
public void stopRecording() { public void stopRecording() {
if (mediaRecorder != null) { if (mediaRecorder != null) {
try {
mediaRecorder.stop(); mediaRecorder.stop();
mediaRecorder.reset(); mediaRecorder.reset();
mediaRecorder.release(); mediaRecorder.release();
mediaRecorder = null; 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();
}
}
} }
} }
public void setSurfaceView(SurfaceView surfaceView) {
this.surfaceView = surfaceView;
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
} }
private String getOutputMediaFilePath() { private void playAudio() {
String videoFileName = "Video.mp4"; MediaPlayer mp = new MediaPlayer();
File storageDir = context.getFilesDir(); try {
mp.setDataSource(context.getFilesDir() + "/audio.gpp");
if (!storageDir.exists()) { mp.prepare();
storageDir.mkdirs(); mp.start();
} catch (Exception e) {
e.printStackTrace();
}
} }
return storageDir.getAbsolutePath() + "/" + videoFileName; public void playVideo(VideoView videoView) {
videoView.setVideoPath(context.getFilesDir() + "/video.mp4");
videoView.start();
} }
} }

View File

@ -27,11 +27,15 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="ToggleButton" /> android:text="ToggleButton" />
<SurfaceView
<androidx.camera.view.PreviewView android:id="@+id/surfaceView"
android:id="@+id/previewView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="400dp" />
android:backgroundTint="@android:color/black"/>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>