From 429e27894859bdadb1e55cac7eeb8dc51fe05410 Mon Sep 17 00:00:00 2001 From: Christian S Date: Thu, 5 May 2016 22:14:56 +0200 Subject: add audio recorder --- src/main/AndroidManifest.xml | 11 ++ .../conversations/ui/ConversationActivity.java | 10 +- .../siacs/conversations/ui/RecordingActivity.java | 159 +++++++++++++++++++++ src/main/res/layout/activity_recording.xml | 52 +++++++ src/main/res/values/strings.xml | 2 + src/main/res/values/themes.xml | 6 + 6 files changed, 235 insertions(+), 5 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/RecordingActivity.java create mode 100644 src/main/res/layout/activity_recording.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 57fdd4c69..9b85f1a4d 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ + + + + + + + = Build.VERSION_CODES.JELLY_BEAN) { + mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); + mRecorder.setAudioEncodingBitRate(48000); + } else { + mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); + mRecorder.setAudioEncodingBitRate(48000); + } + mRecorder.setAudioSamplingRate(48000); + mOutputFile = getOutputFile(); + mOutputFile.getParentFile().mkdirs(); + mRecorder.setOutputFile(mOutputFile.getAbsolutePath()); + + try { + mRecorder.prepare(); + mRecorder.start(); + mStartTime = SystemClock.elapsedRealtime(); + mHandler.postDelayed(mTickExecutor, 100); + Log.d(Config.LOGTAG,"started recording to "+mOutputFile.getAbsolutePath()); + } catch (IOException e) { + Log.e(Config.LOGTAG, "prepare() failed "+e.getMessage()); + } + } + + protected void stopRecording(boolean saveFile) { + mRecorder.stop(); + mRecorder.release(); + mRecorder = null; + mStartTime = 0; + mHandler.removeCallbacks(mTickExecutor); + if (!saveFile && mOutputFile != null) { + mOutputFile.delete(); + } + } + + private File getOutputFile() { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); + return new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString() + + "/Pix-Art Messenger/Recording" + + dateFormat.format(new Date()) + + ".m4a"); + } + + private void tick() { + long time = (mStartTime < 0) ? 0 : (SystemClock.elapsedRealtime() - mStartTime); + int minutes = (int) (time / 60000); + int seconds = (int) (time / 1000) % 60; + int milliseconds = (int) (time / 100) % 10; + mTimerTextView.setText(minutes+":"+(seconds < 10 ? "0"+seconds : seconds)+"."+milliseconds); + if (mRecorder != null) { + amplitudes[i] = mRecorder.getMaxAmplitude(); + //Log.d(Config.LOGTAG,"amplitude: "+(amplitudes[i] * 100 / 32767)); + if (i >= amplitudes.length -1) { + i = 0; + } else { + ++i; + } + } + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.cancel_button: + stopRecording(false); + setResult(RESULT_CANCELED); + finish(); + break; + case R.id.share_button: + stopRecording(true); + Uri uri = Uri.parse("file://"+mOutputFile.getAbsolutePath()); + Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + scanIntent.setData(uri); + sendBroadcast(scanIntent); + setResult(Activity.RESULT_OK, new Intent().setData(uri)); + finish(); + break; + } + } +} \ No newline at end of file diff --git a/src/main/res/layout/activity_recording.xml b/src/main/res/layout/activity_recording.xml new file mode 100644 index 000000000..2e5ea7307 --- /dev/null +++ b/src/main/res/layout/activity_recording.xml @@ -0,0 +1,52 @@ + + + + + +