safedispatch-mobile/src/com/safemobile/lib/sound/AudioHandle.java

204 lines
5.3 KiB
Java
Raw Normal View History

2022-03-10 14:31:03 +00:00
package com.safemobile.lib.sound;
import java.io.DataOutputStream;
import com.safemobile.lib.SM;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.renderscript.Sampler;
import android.widget.Toast;
public class AudioHandle implements Runnable{
private final int SAMPLE_RATE = 8000;
public Boolean isAlive = false;
public Boolean soundNeeded = false;
private AudioRecord recDev =null;
private AudioTrack playDev =null;
private int bufferSize;
private DataOutputStream outData =null;
private Thread t_micListner;
private UDPclient udp;
private TCPaudioClient tcp;
private int audioport = 50001;
public int typeUDP;
public AudioHandle(String IP, int _typeUDP)
{
typeUDP = _typeUDP;
SM.Debug("---AudioHandle construcort---" +_typeUDP);
if(typeUDP==1)
{
try
{
udp = new UDPclient(IP);
bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
int bufsize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
//init play device
//playDev = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,bufsize, AudioTrack.MODE_STREAM);
playDev = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
//init mic listener
recDev = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2);
//recDev = findAudioRecord();
recDev.startRecording();
}
catch(Exception ex)
{
SM.Exception("audioH = 1", ex.toString());
}
}
else
{
try {
tcp = new TCPaudioClient(IP,audioport);
bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
//init play device
playDev = new AudioTrack(AudioManager.STREAM_MUSIC,SAMPLE_RATE,AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize, AudioTrack.MODE_STREAM);
}
catch(Exception ex) {
SM.Exception("audioH = 0 record", ex.toString());
}
try {
//init mic listener
int bufsize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
recDev = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufsize);
//recDev = findAudioRecord();
if(recDev.getState() == AudioRecord.STATE_INITIALIZED)
recDev.startRecording();
/*
else
Toast.makeText(context, "Could not start Recording device (you will not be able to send voice)", Toast.LENGTH_SHORT).show();*/
}
catch(Exception ex) {
SM.Exception("audioH = 0", ex.toString());
//Toast.makeText(context, "Could not start Recording device (you will not be able to send voice)", Toast.LENGTH_SHORT).show();
}
}
isAlive = true;
t_micListner = new Thread(this, "micListener");
t_micListner.start();
if(typeUDP==1)
{
udp.addUDPListener(new IUDPListener()
{
@Override
public void dataRecv(UDPevent event) {
byte[] data = event.data();
int len = event.len();
SM.Debug("recv b:"+len+" data.length:"+data.length);
if(data!=null)
PlaySound(data, len);
}
});
}
else
{
tcp.addTCPListener(new ITCPaudioLis()
{
@Override
public void dataRecv(TCPaudioEvent event) {
byte[] data = event.data();
int len = event.len();
if(data!=null)
PlaySound(data, len);
else
SM.Debug("data ==null");
}
});
}
//playDev.p
SM.Debug("=====AudioHandle construcort ===END====");
}
@Override
public void run() {
byte[] tempBuffer;
int bRead=0;
while(isAlive)
{
try
{
if (soundNeeded)
{
//SM.Debug("sending b:"+bRead);
if(typeUDP==1)
{
tempBuffer = new byte[512];//128];//invers3.bufferSize];
bRead = recDev.read(tempBuffer, 0, 512);// 128);//invers3.bufferSize);
udp.Send(tempBuffer,bRead);
SM.Debug("UDP sending b:"+bRead);
}
else
{
tempBuffer = new byte[2048];//128];//invers3.bufferSize];
bRead = recDev.read(tempBuffer, 0, 2048);// 128);//invers3.bufferSize);
/*
tempBuffer = new byte[16384];//128];//invers3.bufferSize];
bRead = recDev.read(tempBuffer, 0,16384);// 128);//invers3.bufferSize);*/
tcp.Send(tempBuffer,bRead);
SM.Debug("TCP sending b:"+bRead);
}
}
else Thread.sleep(1);
}
catch (Exception e)
{
SM.Debug("break:"+e.toString());
//break;
}
}
}
public void PlaySound(byte[] data, int len)
{
if(playDev.getState() == playDev.STATE_INITIALIZED)
{
playDev.write(data, 0 , len);
playDev.play();
}
else
{
SM.Debug("Cannot play sound playDev NOT init corectly");
}
}
public void StopSound()
{
if(playDev.getState() == playDev.STATE_INITIALIZED)
{
if(playDev.getPlayState() == playDev.PLAYSTATE_PLAYING)
playDev.stop();
}
else
{
SM.Debug("Cannot STOP playDev");
}
}
}