kopia lustrzana https://github.com/sh123/codec2_talkie
Added audio player and audio recorder construction
rodzic
c1b834e570
commit
e408fe781e
|
@ -33,7 +33,7 @@ dependencies {
|
|||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||
implementation 'com.google.android.material:material:1.2.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
|
||||
testImplementation 'junit:junit:4.+'
|
||||
testImplementation 'junit:junit:4.13.1'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.radio.codec2talkie;
|
||||
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
import android.media.AudioAttributes;
|
||||
import android.media.AudioFormat;
|
||||
import android.media.AudioRecord;
|
||||
import android.media.AudioTrack;
|
||||
import android.media.MediaRecorder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class Codec2Player extends Thread {
|
||||
|
||||
private final InputStream _btInputStream;
|
||||
private final OutputStream _btOutputStream;
|
||||
|
||||
private final int AudioSampleRate = 8000;
|
||||
|
||||
private final AudioRecord _audioRecorder;
|
||||
private final int _audioRecorderMinBufferSize;
|
||||
|
||||
private final AudioTrack _audioPlayer;
|
||||
private final int _audioPlayerMinBufferSize;
|
||||
|
||||
public Codec2Player(BluetoothSocket btSocket) throws IOException {
|
||||
|
||||
_btInputStream = btSocket.getInputStream();
|
||||
_btOutputStream = btSocket.getOutputStream();
|
||||
|
||||
_audioRecorderMinBufferSize = AudioRecord.getMinBufferSize(
|
||||
AudioSampleRate,
|
||||
AudioFormat.CHANNEL_IN_MONO,
|
||||
AudioFormat.ENCODING_PCM_16BIT);
|
||||
_audioRecorder = new AudioRecord(
|
||||
MediaRecorder.AudioSource.MIC,
|
||||
AudioSampleRate,AudioFormat.CHANNEL_IN_MONO,
|
||||
AudioFormat.ENCODING_PCM_16BIT,
|
||||
_audioRecorderMinBufferSize);
|
||||
|
||||
_audioPlayerMinBufferSize = AudioTrack.getMinBufferSize(
|
||||
AudioSampleRate,
|
||||
AudioFormat.CHANNEL_OUT_MONO,
|
||||
AudioFormat.ENCODING_PCM_16BIT);
|
||||
_audioPlayer = new AudioTrack.Builder()
|
||||
.setAudioAttributes(new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||
.build())
|
||||
.setAudioFormat(new AudioFormat.Builder()
|
||||
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
|
||||
.setSampleRate(AudioSampleRate)
|
||||
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
|
||||
.build())
|
||||
.setBufferSizeInBytes(_audioPlayerMinBufferSize)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
// read from bluetooth, decode and playback
|
||||
try {
|
||||
int countBytes = _btInputStream.available();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// read from mic, encode and write to bluetooth
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,12 +11,16 @@ import android.widget.AdapterView;
|
|||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private final static int REQUEST_CONNECT_BT = 1;
|
||||
|
||||
private TextView _textBtName;
|
||||
|
||||
private Codec2Player _codec2Player;
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -42,6 +46,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
// Start streaming from mic
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
v.performClick();
|
||||
// Start receiving from bluetooth
|
||||
break;
|
||||
}
|
||||
|
@ -57,6 +62,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
finish();
|
||||
} else if (resultCode == RESULT_OK) {
|
||||
_textBtName.setText(data.getStringExtra("name"));
|
||||
try {
|
||||
_codec2Player = new Codec2Player(BluetoothSocketHandler.getSocket());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,11 @@ allprojects {
|
|||
google()
|
||||
jcenter()
|
||||
}
|
||||
gradle.projectsEvaluated {
|
||||
tasks.withType(JavaCompile) {
|
||||
options.compilerArgs << "-Xlint:deprecation"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
|
|
Ładowanie…
Reference in New Issue