Browse Source

TextureView anstatt SurfaceView

Aufzeichnung
Maria Nutz 1 year ago
parent
commit
316e19236f

+ 8
- 5
app/src/main/java/com/example/greenwatch/AlarmRecorder.java View File

@@ -1,11 +1,13 @@
package com.example.greenwatch;

import android.graphics.SurfaceTexture;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.Surface;
import android.view.SurfaceHolder;

import java.io.File;
@@ -23,23 +25,24 @@ public class AlarmRecorder {
private Runnable videoRecorderRunnable;
private MediaRecorder videoRecorder;
private MediaRecorder audioRecorder;
private SurfaceHolder previewHolder;
//private SurfaceHolder previewHolder;
private Surface surface;
private Thread videoThread; // Video-Thread als Instanzvariable
private Thread audioThread; // Audio-Thread als Instanzvariable

public AlarmRecorder(SurfaceHolder previewHolder) {
public AlarmRecorder(SurfaceTexture surfaceTexture) {
audioRecorder = new MediaRecorder();
videoRecorder = new MediaRecorder();
videoExtractor = new MediaExtractor();
audioExtractor = new MediaExtractor();
this.previewHolder = previewHolder;
this.surface = new Surface(surfaceTexture);
}

public void startRecording() {
createStoragePaths(); //Speicherort und -namen für Audio- und Video-Datei

audioRecorderRunnable = new AudioRecorder(audioRecorder, audioPath);
videoRecorderRunnable = new VideoRecorder(videoRecorder, videoPath, previewHolder);
videoRecorderRunnable = new VideoRecorder(videoRecorder, videoPath, surface);
audioThread = new Thread(audioRecorderRunnable);
videoThread = new Thread(videoRecorderRunnable);

@@ -60,7 +63,7 @@ public class AlarmRecorder {
if (videoFile.exists() && audioFile.exists()) {
//Wenn Video- und Audioaufzeichnung gestoppt und abgespeichert sind, beginne mit dem Mergeprozess der beiden
mergeVideoWithAudio();
} else { }
}
} catch (RuntimeException stopException) {
stopException.printStackTrace();
}

+ 34
- 6
app/src/main/java/com/example/greenwatch/MainActivity.java View File

@@ -2,8 +2,9 @@ package com.example.greenwatch;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@@ -12,14 +13,13 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{

private boolean isRecording = false;
private static final int REQUEST_PERMISSION = 200;
private Button button;
private SurfaceView surfaceView;
private TextureView textureView;
private AlarmRecorder alarmRecorder;
private static final int REQUEST_PERMISSIONS = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -27,8 +27,8 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);

button = findViewById(R.id.button);
surfaceView = findViewById(R.id.surfaceView);
alarmRecorder = new AlarmRecorder(surfaceView.getHolder());
textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);

button.setOnClickListener(new View.OnClickListener() {
@Override
@@ -55,6 +55,34 @@ public class MainActivity extends AppCompatActivity {
});
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
alarmRecorder = new AlarmRecorder(surfaceTexture);
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Die Größe der SurfaceTexture hat sich geändert
// Hier können entsprechende Anpassungen vorgenommen werden
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
// SurfaceTexture wurde zerstört
// Hier können entsprechende Bereinigungen durchgeführt werden
return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// SurfaceTexture wurde aktualisiert
// Hier können entsprechende Aktionen ausgeführt werden, wenn gewünscht
}

private SurfaceTexture getSurfaceTexture() {
return textureView.getSurfaceTexture();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

+ 7
- 4
app/src/main/java/com/example/greenwatch/VideoRecorder.java View File

@@ -1,6 +1,7 @@
package com.example.greenwatch;

import android.media.MediaRecorder;
import android.view.Surface;
import android.view.SurfaceHolder;

import java.io.IOException;
@@ -8,12 +9,14 @@ import java.io.IOException;
public class VideoRecorder implements Runnable{
private final MediaRecorder videoRecorder;
private final String videoPath;
private SurfaceHolder previewHolder;
//private SurfaceHolder previewHolder;
private Surface surface;

public VideoRecorder(MediaRecorder videoRecorder, String videoPath, SurfaceHolder previewHolder) {

public VideoRecorder(MediaRecorder videoRecorder, String videoPath, Surface surface) {
this.videoRecorder = videoRecorder;
this.videoPath = videoPath;
this.previewHolder = previewHolder;
this.surface = surface;
}
@Override
public void run() {
@@ -22,7 +25,7 @@ public class VideoRecorder implements Runnable{
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
videoRecorder.setOutputFile(videoPath);
videoRecorder.setOrientationHint(90);
videoRecorder.setPreviewDisplay(previewHolder.getSurface());
videoRecorder.setPreviewDisplay(surface);

try {
videoRecorder.prepare();

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

@@ -6,16 +6,16 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="1dp"
android:layout_height="1dp"
<TextureView
android:id="@+id/textureView"
android:layout_width="1sp"
android:layout_height="1sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="@id/button"
app:layout_constraintVertical_bias="0.0" />

<Button

Loading…
Cancel
Save