Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
144b8eb676 | ||
|
|
d3d7def833 | ||
|
|
c61e56c3b5 | ||
|
|
f2b85fb4e7 | ||
|
|
32decab687 | ||
|
|
f98a3580a6 | ||
|
|
5b920359a1 |
@@ -2,13 +2,7 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.example.greenwatch">
|
package="com.example.greenwatch">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
|
||||||
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
|
|
||||||
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
|
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
@@ -16,6 +10,9 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.GreenWatch">
|
android:theme="@style/Theme.GreenWatch">
|
||||||
|
<activity
|
||||||
|
android:name=".Mikrofon"
|
||||||
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
|
|||||||
@@ -1,225 +0,0 @@
|
|||||||
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;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
public class AlarmRecorder {
|
|
||||||
private String videoPath;
|
|
||||||
private String audioPath;
|
|
||||||
private MediaFormat videoFormat;
|
|
||||||
private MediaFormat audioFormat;
|
|
||||||
private MediaExtractor videoExtractor;
|
|
||||||
private MediaExtractor audioExtractor;
|
|
||||||
private Runnable audioRecorderRunnable;
|
|
||||||
private Runnable videoRecorderRunnable;
|
|
||||||
private MediaRecorder videoRecorder;
|
|
||||||
private MediaRecorder audioRecorder;
|
|
||||||
//private SurfaceHolder previewHolder;
|
|
||||||
private Surface surface;
|
|
||||||
private Thread videoThread; // Video-Thread als Instanzvariable
|
|
||||||
private Thread audioThread; // Audio-Thread als Instanzvariable
|
|
||||||
|
|
||||||
public AlarmRecorder(SurfaceTexture surfaceTexture) {
|
|
||||||
audioRecorder = new MediaRecorder();
|
|
||||||
videoRecorder = new MediaRecorder();
|
|
||||||
videoExtractor = new MediaExtractor();
|
|
||||||
audioExtractor = new MediaExtractor();
|
|
||||||
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, surface);
|
|
||||||
audioThread = new Thread(audioRecorderRunnable);
|
|
||||||
videoThread = new Thread(videoRecorderRunnable);
|
|
||||||
|
|
||||||
//Threads starten
|
|
||||||
videoThread.start();
|
|
||||||
audioThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopRecording() {
|
|
||||||
try {
|
|
||||||
stopVideoRecording();
|
|
||||||
stopAudioRecording();
|
|
||||||
|
|
||||||
waitTillThreadsStopped();
|
|
||||||
File videoFile = new File(videoPath); //Speichere das aufgenommene Video
|
|
||||||
File audioFile = new File(audioPath); //Speichere die aufgenommene Audio
|
|
||||||
|
|
||||||
if (videoFile.exists() && audioFile.exists()) {
|
|
||||||
//Wenn Video- und Audioaufzeichnung gestoppt und abgespeichert sind, beginne mit dem Mergeprozess der beiden
|
|
||||||
mergeVideoWithAudio();
|
|
||||||
}
|
|
||||||
} catch (RuntimeException stopException) {
|
|
||||||
stopException.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createStoragePaths(){
|
|
||||||
//Pfade zum Zwischenspeichern der aufgenommenen Audio und Video-Datei
|
|
||||||
String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
||||||
String dcimDirectory = externalStorageDirectory + "/DCIM";
|
|
||||||
videoPath = dcimDirectory + "/video.mp4";
|
|
||||||
audioPath = dcimDirectory + "/audio.mp3";
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stopVideoRecording(){
|
|
||||||
if (videoRecorder != null) {
|
|
||||||
videoRecorder.stop();
|
|
||||||
videoRecorder.release();
|
|
||||||
videoRecorder = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stopAudioRecording(){
|
|
||||||
if (audioRecorder != null) {
|
|
||||||
audioRecorder.stop();
|
|
||||||
audioRecorder.release();
|
|
||||||
audioRecorder = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void waitTillThreadsStopped(){
|
|
||||||
try {
|
|
||||||
videoThread.join();
|
|
||||||
audioThread.join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void mergeVideoWithAudio() {
|
|
||||||
try {
|
|
||||||
setVideoExtractorDataSource(); //extrahieren der Video Datei, die zuvor zwischengespeichert wurde
|
|
||||||
setAudioExtractorDataSource(); //extrahieren der Audio Datei, die zuvor zwischengespeichert wurde
|
|
||||||
|
|
||||||
//Speicherort der später zusammengeführten Datei
|
|
||||||
String outputFilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath() + "/merged_video.mp4";
|
|
||||||
//MediaMuxer zum Zusammenführen einer Audio- und einer Videodatei
|
|
||||||
MediaMuxer muxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
|
|
||||||
int videoTrack = muxer.addTrack(videoFormat);
|
|
||||||
int audioTrack = muxer.addTrack(audioFormat);
|
|
||||||
muxer.start();
|
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
|
|
||||||
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
|
|
||||||
|
|
||||||
videoExtraction(buffer, videoTrack, bufferInfo, muxer);
|
|
||||||
audioExtraction(buffer, audioTrack, bufferInfo, muxer);
|
|
||||||
|
|
||||||
muxer.stop();
|
|
||||||
muxer.release();
|
|
||||||
|
|
||||||
// Löschen der separaten Video- und Audio-Dateien
|
|
||||||
deleteVideoFile();
|
|
||||||
deleteAudioFile();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setVideoExtractorDataSource() {
|
|
||||||
try {
|
|
||||||
videoExtractor.setDataSource(videoPath);
|
|
||||||
int videoTrackIndex = getTrackIndex(videoExtractor, "video/");
|
|
||||||
if (videoTrackIndex < 0) {
|
|
||||||
// Video-Track nicht gefunden
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
videoExtractor.selectTrack(videoTrackIndex);
|
|
||||||
videoFormat = videoExtractor.getTrackFormat(videoTrackIndex);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setAudioExtractorDataSource() {
|
|
||||||
try {
|
|
||||||
audioExtractor.setDataSource(audioPath);
|
|
||||||
int audioTrackIndex = getTrackIndex(audioExtractor, "audio/");
|
|
||||||
if (audioTrackIndex < 0) {
|
|
||||||
// Audio-Track nicht gefunden
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
audioExtractor.selectTrack(audioTrackIndex);
|
|
||||||
audioFormat = audioExtractor.getTrackFormat(audioTrackIndex);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getTrackIndex(MediaExtractor extractor, String mimeType) {
|
|
||||||
int trackCount = extractor.getTrackCount();
|
|
||||||
for (int i = 0; i < trackCount; i++) {
|
|
||||||
MediaFormat format = extractor.getTrackFormat(i);
|
|
||||||
String trackMimeType = format.getString(MediaFormat.KEY_MIME);
|
|
||||||
if (trackMimeType.startsWith(mimeType)) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void videoExtraction(ByteBuffer buffer, int videoTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
|
|
||||||
videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
|
|
||||||
while (true) {
|
|
||||||
int sampleSize = videoExtractor.readSampleData(buffer, 0);
|
|
||||||
if (sampleSize < 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
long presentationTimeUs = videoExtractor.getSampleTime();
|
|
||||||
bufferInfo.offset = 0;
|
|
||||||
bufferInfo.size = sampleSize;
|
|
||||||
bufferInfo.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME;
|
|
||||||
bufferInfo.presentationTimeUs = presentationTimeUs;
|
|
||||||
muxer.writeSampleData(videoTrack, buffer, bufferInfo);
|
|
||||||
videoExtractor.advance();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void audioExtraction(ByteBuffer buffer, int audioTrack, MediaCodec.BufferInfo bufferInfo, MediaMuxer muxer) {
|
|
||||||
audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
|
|
||||||
while (true) {
|
|
||||||
int sampleSize = audioExtractor.readSampleData(buffer, 0);
|
|
||||||
if (sampleSize < 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
long presentationTimeUs = audioExtractor.getSampleTime();
|
|
||||||
bufferInfo.offset = 0;
|
|
||||||
bufferInfo.size = sampleSize;
|
|
||||||
bufferInfo.flags = 0; // or MediaCodec.BUFFER_FLAG_KEY_FRAME
|
|
||||||
bufferInfo.presentationTimeUs = presentationTimeUs;
|
|
||||||
muxer.writeSampleData(audioTrack, buffer, bufferInfo);
|
|
||||||
audioExtractor.advance();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteVideoFile(){
|
|
||||||
File videoFile = new File(videoPath);
|
|
||||||
if (videoFile.exists()) {
|
|
||||||
videoFile.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteAudioFile(){
|
|
||||||
File audioFile = new File(audioPath);
|
|
||||||
if (audioFile.exists()) {
|
|
||||||
audioFile.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
import android.media.MediaRecorder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class AudioRecorder implements Runnable {
|
|
||||||
|
|
||||||
private final MediaRecorder audioRecorder;
|
|
||||||
private final String audioPath;
|
|
||||||
public AudioRecorder(MediaRecorder audioRecorder, String audioPath) {
|
|
||||||
this.audioRecorder = audioRecorder;
|
|
||||||
this.audioPath = audioPath;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
audioRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
|
|
||||||
audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
|
||||||
audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
|
|
||||||
audioRecorder.setOutputFile(audioPath);
|
|
||||||
|
|
||||||
try {
|
|
||||||
audioRecorder.prepare();
|
|
||||||
audioRecorder.start();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
public class Beschleunigungssensor {
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
public class GleitenderMittelwert {
|
||||||
|
private final float wichtungNeuerWert;
|
||||||
|
private final float wichtungAlterWert;
|
||||||
|
private float mittelwert = 0;
|
||||||
|
|
||||||
|
private boolean istMittelwertGesetzt = false;
|
||||||
|
|
||||||
|
GleitenderMittelwert(float wichtungNeuerWert) {
|
||||||
|
this.wichtungNeuerWert = wichtungNeuerWert;
|
||||||
|
this.wichtungAlterWert = 1 - this.wichtungNeuerWert;
|
||||||
|
}
|
||||||
|
|
||||||
|
float mittel(float wert) {
|
||||||
|
if (istMittelwertGesetzt) {
|
||||||
|
mittelwert = wert * wichtungNeuerWert + mittelwert * wichtungAlterWert;
|
||||||
|
} else {
|
||||||
|
mittelwert = wert;
|
||||||
|
istMittelwertGesetzt = true;
|
||||||
|
}
|
||||||
|
return mittelwert;
|
||||||
|
}}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
public class Kamera {
|
||||||
|
}
|
||||||
@@ -1,101 +1,25 @@
|
|||||||
package com.example.greenwatch;
|
package com.example.greenwatch;
|
||||||
|
|
||||||
import android.Manifest;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.graphics.SurfaceTexture;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.TextureView;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
public class MainActivity extends AppCompatActivity {
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{
|
|
||||||
|
|
||||||
private boolean isRecording = false;
|
|
||||||
private static final int REQUEST_PERMISSION = 200;
|
|
||||||
private Button button;
|
|
||||||
private TextureView textureView;
|
|
||||||
private AlarmRecorder alarmRecorder;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
button = findViewById(R.id.button);
|
((Button)findViewById(R.id.buttonstartmicdetection)).setOnClickListener(new View.OnClickListener(){
|
||||||
textureView = findViewById(R.id.textureView);
|
|
||||||
textureView.setSurfaceTextureListener(this);
|
|
||||||
|
|
||||||
button.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View view) {
|
||||||
if (!isRecording) {
|
Intent i = new Intent(MainActivity.this, Mikrofon.class);
|
||||||
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
|
startActivity(i);
|
||||||
!= PackageManager.PERMISSION_GRANTED
|
|
||||||
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
||||||
!= PackageManager.PERMISSION_GRANTED
|
|
||||||
|| ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)
|
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
|
||||||
ActivityCompat.requestPermissions(MainActivity.this,
|
|
||||||
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},
|
|
||||||
REQUEST_PERMISSION);
|
|
||||||
} else {
|
|
||||||
isRecording = true;
|
|
||||||
alarmRecorder.startRecording();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
isRecording = false;
|
|
||||||
alarmRecorder.stopRecording();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
|
||||||
if (requestCode == REQUEST_PERMISSION) {
|
|
||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
// Berechtigungen wurden gewährt
|
|
||||||
Toast.makeText(this, "Berechtigungen gewährt", Toast.LENGTH_SHORT).show();
|
|
||||||
isRecording = true;
|
|
||||||
alarmRecorder.startRecording();
|
|
||||||
} else {
|
|
||||||
// Berechtigungen wurden nicht gewährt
|
|
||||||
Toast.makeText(this, "Berechtigungen nicht gewährt", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,254 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.media.AudioFormat;
|
||||||
|
import android.media.AudioRecord;
|
||||||
|
import android.media.MediaRecorder;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Mikrofon extends AppCompatActivity {
|
||||||
|
|
||||||
|
private AufnahmeTask aufnahmeTask;
|
||||||
|
private TextView textViewMaxAmp;
|
||||||
|
private TextView textViewMaxAmpdb;
|
||||||
|
private TextView textViewZeitstempel;
|
||||||
|
private static final int RECHTEANFORDERUNG_MIKROFON = 1;
|
||||||
|
|
||||||
|
private boolean detection_mic = false;
|
||||||
|
private String zeit = "0";
|
||||||
|
private boolean kalibrierung_do = true;
|
||||||
|
private float threshold = 40;
|
||||||
|
private float sensitivity = 5;
|
||||||
|
private final static String KEY_WERT = "KEY_WERT_" + Mikrofon.class.getSimpleName();
|
||||||
|
private final static String KEY_KALIBRIERUNG = "KEY_KALIBRIERUNG_" + Mikrofon.class.getSimpleName();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_mikrofon);
|
||||||
|
textViewMaxAmp = (TextView) findViewById(R.id.textViewMaxAmp);
|
||||||
|
textViewMaxAmpdb = (TextView) findViewById(R.id.textViewMaxAmpdb);
|
||||||
|
textViewZeitstempel = (TextView) findViewById(R.id.textViewZeitstempel);
|
||||||
|
//kalibrierung_do = true;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
SharedPreferences p = getPreferences(Context.MODE_PRIVATE);
|
||||||
|
threshold = p.getFloat(KEY_WERT, 40);
|
||||||
|
kalibrierung_do = p.getBoolean(KEY_KALIBRIERUNG, true);
|
||||||
|
|
||||||
|
if (!istZugriffAufMikrofonErlaubt()) {
|
||||||
|
zugriffAufMikrofonAnfordern();
|
||||||
|
}
|
||||||
|
if (istZugriffAufMikrofonErlaubt()) {
|
||||||
|
aufnahmeTask = new AufnahmeTask();
|
||||||
|
aufnahmeTask.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
SharedPreferences p = getPreferences(Context.MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = p.edit();
|
||||||
|
editor.putFloat(KEY_WERT, threshold);
|
||||||
|
if(isFinishing()==true){
|
||||||
|
editor.putBoolean(KEY_KALIBRIERUNG, true);
|
||||||
|
}else {
|
||||||
|
editor.putBoolean(KEY_KALIBRIERUNG, false);
|
||||||
|
}
|
||||||
|
editor.commit();
|
||||||
|
if(aufnahmeTask!=null) {
|
||||||
|
aufnahmeTask.cancel(true);
|
||||||
|
aufnahmeTask = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean istZugriffAufMikrofonErlaubt() {
|
||||||
|
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void zugriffAufMikrofonAnfordern() {
|
||||||
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, RECHTEANFORDERUNG_MIKROFON);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String Zeitpunkt(){
|
||||||
|
Long tslong = System.currentTimeMillis();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
|
||||||
|
Date date = new Date(tslong);
|
||||||
|
String resultdate = sdf.format(date);
|
||||||
|
|
||||||
|
return resultdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class AufnahmeTask extends AsyncTask<Long, Verarbeitungsergebnis, Void> {
|
||||||
|
private AudioRecord recorder;
|
||||||
|
private final int sampleRateInHz = 44100;
|
||||||
|
private final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
|
||||||
|
private final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
|
||||||
|
private int minPufferGroesseInBytes;
|
||||||
|
private int pufferGroesseInBytes;
|
||||||
|
private RingPuffer ringPuffer = new RingPuffer(10);
|
||||||
|
private boolean erlaubt_rise = true;
|
||||||
|
private boolean erlaubt_fall = true;
|
||||||
|
|
||||||
|
AufnahmeTask() {
|
||||||
|
minPufferGroesseInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
|
||||||
|
pufferGroesseInBytes = minPufferGroesseInBytes * 2;
|
||||||
|
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
|
||||||
|
}
|
||||||
|
//hi
|
||||||
|
public float kalibrieren(short[] puffer){
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
int anzahlIterationen = 100;
|
||||||
|
float sum = 0;
|
||||||
|
for(int i = 0; i < anzahlIterationen; i++){
|
||||||
|
int z = recorder.read(puffer, 0, puffer.length);
|
||||||
|
Verarbeitungsergebnis kalibrierung = verarbeiten(puffer, z);
|
||||||
|
sum += (float) kalibrierung.db;
|
||||||
|
System.out.println("Aktueller_Wert Kalibrierung" +kalibrierung.db);
|
||||||
|
}
|
||||||
|
System.out.println("Summe:"+sum);
|
||||||
|
return sum/anzahlIterationen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Detektion(Verarbeitungsergebnis ergebnis){
|
||||||
|
if(ergebnis.db >= (threshold+sensitivity)){
|
||||||
|
detection_mic = true;
|
||||||
|
erlaubt_fall = true;
|
||||||
|
while(erlaubt_rise){
|
||||||
|
erlaubt_rise = false;
|
||||||
|
zeit = Zeitpunkt() + " - " + ergebnis.db + " - " + (threshold+sensitivity); //Überschreiben des Zeitpunkts beim Erkennen der Detektion
|
||||||
|
//nur zum Testen in zeit noch ergebnis.db und threshold ausgegeben -> muss am Ende nur Zeitpunkt() rein, Rest weg
|
||||||
|
}
|
||||||
|
} else if (ergebnis.db <= (threshold)) {
|
||||||
|
detection_mic = false;
|
||||||
|
erlaubt_rise = true;
|
||||||
|
while(erlaubt_fall){
|
||||||
|
erlaubt_fall = false;
|
||||||
|
zeit = Zeitpunkt(); //erneutes Überschreiben des Zeitpunkts beim Verschwinden der Detektion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Long... params) {
|
||||||
|
|
||||||
|
recorder.startRecording();
|
||||||
|
short[] puffer = new short[pufferGroesseInBytes / 2];
|
||||||
|
GleitenderMittelwert gleitenderMittelwertdB = new GleitenderMittelwert(0.2f);
|
||||||
|
float db = 0;
|
||||||
|
|
||||||
|
|
||||||
|
//kalibrierung
|
||||||
|
if(kalibrierung_do){
|
||||||
|
threshold = kalibrieren(puffer);
|
||||||
|
kalibrierung_do = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (; ; ) {
|
||||||
|
if (isCancelled()) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
//ergebnis ermitteln
|
||||||
|
int n = recorder.read(puffer, 0, puffer.length);
|
||||||
|
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n);
|
||||||
|
|
||||||
|
//Mittelwertberechnnung
|
||||||
|
db = (float) ergebnis.db;
|
||||||
|
db = gleitenderMittelwertdB.mittel(db);
|
||||||
|
ergebnis.db = (int) db;
|
||||||
|
|
||||||
|
//Db Wert mit Schwellwert vergleichen und Warnung setzen
|
||||||
|
Detektion(ergebnis);
|
||||||
|
publishProgress(ergebnis);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(10);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recorder.release();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Verarbeitungsergebnis verarbeiten(short[] daten, int n) {
|
||||||
|
String status;
|
||||||
|
short maxAmp = -1;
|
||||||
|
int db = 0, db_wert = 0;
|
||||||
|
|
||||||
|
if (n == AudioRecord.ERROR_INVALID_OPERATION) {
|
||||||
|
status = "ERROR_INVALID_OPERATION";
|
||||||
|
} else if (n == AudioRecord.ERROR_BAD_VALUE) {
|
||||||
|
status = "ERROR_BAD_VALUE";
|
||||||
|
} else {
|
||||||
|
status = "OK";
|
||||||
|
short max = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (daten[i] > max) {
|
||||||
|
max = daten[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ringPuffer.hinzufuegen(max);
|
||||||
|
maxAmp = ringPuffer.maximum();
|
||||||
|
}
|
||||||
|
//Umwandlung Amplitudenwert in dB
|
||||||
|
db_wert = (int) (20*Math.log10(maxAmp));
|
||||||
|
if (db_wert > 0){
|
||||||
|
db = db_wert;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return new Verarbeitungsergebnis(status, maxAmp, db);
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////
|
||||||
|
@Override
|
||||||
|
protected void onProgressUpdate(Verarbeitungsergebnis... progress) {
|
||||||
|
super.onProgressUpdate(progress);
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(detection_mic);
|
||||||
|
System.out.println(zeit);
|
||||||
|
textViewZeitstempel.setText(zeit);
|
||||||
|
|
||||||
|
textViewMaxAmpdb.setText("" + progress[0].db);
|
||||||
|
textViewMaxAmp.setText("" + progress[0].maxAmp);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/////////////////////////////////////////////
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
public class RingPuffer {
|
||||||
|
|
||||||
|
private short[] puffer;
|
||||||
|
private final int laenge;
|
||||||
|
private int anzahlEnthaltenerDaten;
|
||||||
|
private int position;
|
||||||
|
|
||||||
|
|
||||||
|
public RingPuffer(int n) {
|
||||||
|
laenge = n;
|
||||||
|
anzahlEnthaltenerDaten = 0;
|
||||||
|
position = 0;
|
||||||
|
puffer = new short[laenge];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hinzufuegen(short wert) {
|
||||||
|
puffer[position] = wert;
|
||||||
|
position++;
|
||||||
|
if (position >= laenge) {
|
||||||
|
position = 0;
|
||||||
|
}
|
||||||
|
if (anzahlEnthaltenerDaten < laenge) {
|
||||||
|
anzahlEnthaltenerDaten++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public void hinzufuegen(short[] daten) {
|
||||||
|
for (short d : daten) {
|
||||||
|
puffer[position] = d;
|
||||||
|
position++;
|
||||||
|
if (position >= laenge) {
|
||||||
|
position = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (anzahlEnthaltenerDaten < laenge) {
|
||||||
|
anzahlEnthaltenerDaten += daten.length;
|
||||||
|
if (anzahlEnthaltenerDaten >= laenge) {
|
||||||
|
anzahlEnthaltenerDaten = laenge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public short maximum() {
|
||||||
|
short max = 0;
|
||||||
|
for (int i = 0; i < anzahlEnthaltenerDaten; i++) {
|
||||||
|
if (puffer[i] > max) {
|
||||||
|
max = puffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* public float mittelwert() {
|
||||||
|
float summe = 0;
|
||||||
|
for (int i = 0; i < anzahlEnthaltenerDaten; i++) {
|
||||||
|
summe += puffer[i];
|
||||||
|
}
|
||||||
|
return summe / anzahlEnthaltenerDaten;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.greenwatch;
|
||||||
|
|
||||||
|
public class Verarbeitungsergebnis {
|
||||||
|
String status;
|
||||||
|
short maxAmp;
|
||||||
|
int db;
|
||||||
|
//
|
||||||
|
Verarbeitungsergebnis(String status, short maxAmp, int db) {
|
||||||
|
this.status = status;
|
||||||
|
this.maxAmp = maxAmp;
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package com.example.greenwatch;
|
|
||||||
|
|
||||||
import android.media.MediaRecorder;
|
|
||||||
import android.view.Surface;
|
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class VideoRecorder implements Runnable{
|
|
||||||
private final MediaRecorder videoRecorder;
|
|
||||||
private final String videoPath;
|
|
||||||
//private SurfaceHolder previewHolder;
|
|
||||||
private Surface surface;
|
|
||||||
|
|
||||||
|
|
||||||
public VideoRecorder(MediaRecorder videoRecorder, String videoPath, Surface surface) {
|
|
||||||
this.videoRecorder = videoRecorder;
|
|
||||||
this.videoPath = videoPath;
|
|
||||||
this.surface = surface;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
|
|
||||||
videoRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
|
|
||||||
videoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
|
|
||||||
videoRecorder.setOutputFile(videoPath);
|
|
||||||
videoRecorder.setOrientationHint(90);
|
|
||||||
videoRecorder.setPreviewDisplay(surface);
|
|
||||||
|
|
||||||
try {
|
|
||||||
videoRecorder.prepare();
|
|
||||||
videoRecorder.start();
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -6,26 +6,13 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
<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_toBottomOf="@id/button"
|
|
||||||
app:layout_constraintVertical_bias="0.0" />
|
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/button"
|
android:id="@+id/buttonstartmicdetection"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginEnd="339dp"
|
android:text="Starte Mikrofon Detection"
|
||||||
android:text="Record Video"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent"/>
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:paddingBottom="16dp"
|
||||||
|
android:paddingLeft="16dp"
|
||||||
|
android:paddingRight="16dp"
|
||||||
|
android:paddingTop="16dp"
|
||||||
|
tools:activity="com.example.greenwatch">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="max. Amp: "
|
||||||
|
android:id="@+id/labelMaxAmp"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_alignParentStart="true"/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:id="@+id/textViewMaxAmp"
|
||||||
|
android:text=" "
|
||||||
|
android:layout_alignBaseline="@+id/labelMaxAmp"
|
||||||
|
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
|
||||||
|
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="max. Amp in dB: "
|
||||||
|
android:id="@+id/labelMaxAmpdb"
|
||||||
|
android:layout_below="@+id/labelMaxAmp"
|
||||||
|
android:layout_alignParentStart="true"/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:id="@+id/textViewMaxAmpdb"
|
||||||
|
android:text=" "
|
||||||
|
android:layout_alignBaseline="@+id/labelMaxAmpdb"
|
||||||
|
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Meldung: "
|
||||||
|
android:id="@+id/labelZeitstempel"
|
||||||
|
android:layout_below="@+id/labelMaxAmpdb"
|
||||||
|
android:layout_alignParentStart="true"/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:id="@+id/textViewZeitstempel"
|
||||||
|
android:text=" "
|
||||||
|
android:layout_alignBaseline="@+id/labelZeitstempel"
|
||||||
|
android:layout_toEndOf="@+id/labelMaxAmpdb"/>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/scrollView3"
|
||||||
|
android:layout_below="@+id/labelMaxAmpdb"
|
||||||
|
android:layout_alignParentStart="true">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:id="@+id/textViewLogger"/>
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
Reference in New Issue
Block a user