Added checkbox for changing audio source between VOICE and COMMUNICATION

This commit is contained in:
jima 2017-09-06 11:47:06 +02:00
parent 60718b503c
commit 4aaae88148
4 changed files with 59 additions and 12 deletions

View file

@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

View file

@ -25,6 +25,7 @@ import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ExpandedMenuView;
@ -34,6 +35,7 @@ import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
@ -63,6 +65,7 @@ public class MainActivity extends AppCompatActivity {
private Button mPlayRecording;
private float iGain = 1.0f;
private TextView mLog;
private CheckBox mAudioSourceCb;
protected int bitsPerSamples = 16;
@ -73,6 +76,7 @@ public class MainActivity extends AppCompatActivity {
private int mFileCounter = 1;
private boolean mIsRecording = false;
private boolean mStartup = true;
private VerifyPermissions mVerifyPermissions;
private BluetoothAdapter mBluetoothAdapter;
@ -89,6 +93,20 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mStartup = false;
}
}, 111);
}
private void initialize() {
setContentView(R.layout.activity_main);
@ -104,6 +122,7 @@ public class MainActivity extends AppCompatActivity {
mStopRecordAudioBtn = (Button) findViewById(R.id.btn_stop_record_audio);
mRecordAudioBtn.setOnClickListener(mRecordAudioClickListener);
mStopRecordAudioBtn.setOnClickListener(mStopRecordAudioClickListener);
mAudioSourceCb = (CheckBox) findViewById(R.id.audioSource);
File dir = new File(System.getenv("EXTERNAL_STORAGE") + "/" + SOUND_PATH);
if (dir.exists()) {
@ -243,6 +262,7 @@ public class MainActivity extends AppCompatActivity {
mRecordAudioBtn.setEnabled(false);
mStopRecordAudioBtn.setEnabled(true);
mAudioSourceCb.setEnabled(false);
if (mRecordAudioBtn.getText().equals(getString(R.string.record_start_bt))) {
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
@ -268,6 +288,7 @@ public class MainActivity extends AppCompatActivity {
mRecordAudioBtn.setEnabled(true);
mStopRecordAudioBtn.setEnabled(false);
mAudioSourceCb.setEnabled(true);
log("-- stop recording");
@ -313,17 +334,22 @@ public class MainActivity extends AppCompatActivity {
if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
log("disconnected...");
log("BT disconnected...");
stopRecording();
}
else if (state == AudioManager.SCO_AUDIO_STATE_CONNECTING) {
log("connecting...");
log("BT connecting...");
}
else if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
log("connected...");
startRecording();
log("BT connected...");
if (mStartup) {
mStartup = false;
} else {
startRecording();
}
}
}
};
@ -336,15 +362,25 @@ public class MainActivity extends AppCompatActivity {
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE
, (isStereo ? AudioFormat.CHANNEL_IN_STEREO
: AudioFormat.CHANNEL_IN_MONO)
: AudioFormat.CHANNEL_IN_MONO)
, AudioFormat.ENCODING_PCM_16BIT);
mBuffer = new short[bufferSize];
mRecorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION
, SAMPLE_RATE
, (isStereo ? AudioFormat.CHANNEL_IN_STEREO
: AudioFormat.CHANNEL_IN_MONO)
, AudioFormat.ENCODING_PCM_16BIT
, bufferSize);
final int audioSource;
if (mAudioSourceCb.isChecked()) {
log("Audio Source: VOICE_RECOGNITION");
audioSource = MediaRecorder.AudioSource.VOICE_RECOGNITION;
} else {
log("Audio Source: VOICE_COMMUNICATION");
audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
}
mRecorder = new AudioRecord(audioSource
, SAMPLE_RATE
, (isStereo ? AudioFormat.CHANNEL_IN_STEREO
: AudioFormat.CHANNEL_IN_MONO)
, AudioFormat.ENCODING_PCM_16BIT
, bufferSize);
try {
mRecorder.startRecording();
@ -377,6 +413,7 @@ public class MainActivity extends AppCompatActivity {
} else {
mStopRecordAudioBtn.setEnabled(false);
mRecordAudioBtn.setEnabled(true);
mAudioSourceCb.setEnabled(true);
}
}

View file

@ -1,4 +1,5 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -57,6 +58,13 @@
android:enabled="false"
android:text="@string/record_stop" />
<CheckBox
android:id="@+id/audioSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/audio_sources"/>
<TextView
android:id="@+id/log"
android:layout_width="match_parent"

View file

@ -24,5 +24,7 @@
<string name="text_list_paired_devices">Paired Devices</string>
<string name="text_list_devices">List of Devices</string>
<string name="audio_sources">VOICE_RECOGNITION (checked) / VOICE_COMMUNICATION (unchecked)</string>
<string name="text_pair">Pair</string>
</resources>