|
|
|
|
|
|
|
|
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 androidx.camera.core.Camera; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
|
import android.view.SurfaceHolder; |
|
|
|
|
|
import android.view.SurfaceView; |
|
|
|
|
|
import android.widget.VideoView; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
public class Recorder { |
|
|
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) { |
|
|
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(); |
|
|
|
|
|
|
|
|
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.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) { |
|
|
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(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |