-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
809806c
commit 8e70760
Showing
5 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/de/delbertooo/careco/android/preheatingcontrol/Recorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package de.delbertooo.careco.android.preheatingcontrol; | ||
|
||
import android.media.AudioFormat; | ||
import android.media.AudioRecord; | ||
import android.media.MediaRecorder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by robert on 05.03.17. | ||
*/ | ||
|
||
public class Recorder { | ||
private static final int RECORDER_SAMPLE_RATE = 8000; | ||
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; | ||
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; | ||
private static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING); | ||
private AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLE_RATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, BUFFER_SIZE); | ||
private boolean recording = false; | ||
private Thread readThread; | ||
private List<Short> result = new ArrayList<>(); | ||
|
||
public void start() { | ||
if (recording) { | ||
throw new RuntimeException("Already recording."); | ||
} | ||
recording = true; | ||
recorder.startRecording(); | ||
readThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
short buffer[] = new short[BUFFER_SIZE]; | ||
while (recording) { | ||
int bytesRead = recorder.read(buffer, 0, BUFFER_SIZE); | ||
for (int i = 0; i < bytesRead; i++) { | ||
result.add(buffer[i]); | ||
} | ||
} | ||
} | ||
}); | ||
readThread.start(); | ||
} | ||
|
||
public void stop() { | ||
recording = false; | ||
try { | ||
readThread.join(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException("Failed to stop recording.", e); | ||
} | ||
recorder.stop(); | ||
recorder.release(); | ||
readThread = null; | ||
} | ||
|
||
public List<Short> getResult() { | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters