есть такой код для проигрывания аудио сигнала который пришел с udp сокета
public int SAMPLE_RATE = 44100;
public int CHANNEL = AudioFormat.CHANNEL_OUT_MONO;
public int bfsze = AudioTrack.getMinBufferSize(SAMPLE_RATE,CHANNEL,AudioFormat.ENCODING_PCM_16BIT);
public void startSpeakers() {
// Creates the thread for receiving and playing back audio
if (bfsze == AudioTrack.ERROR || bfsze == AudioTrack.ERROR_BAD_VALUE){
//bfsze = SAMPLE_RATE*2;
}
if(!speakers) {
Toast.makeText(getApplicationContext(),"ok start "+bfsze,Toast.LENGTH_SHORT).show();
speakers = true;
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
// Create an instance of AudioTrack, used for playing back audio
Log.i(LOG_TAG, "Receive thread started. Thread id: " + Thread.currentThread().getId());
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, CHANNEL,
AudioFormat.ENCODING_PCM_16BIT, bfsze, AudioTrack.MODE_STREAM);
track.play();
try {
// Define a socket to receive the audio
DatagramSocket socket = new DatagramSocket(port);
byte[] buf = new byte[bfsze];
while(speakers) {
// Play back the audio received from packets
DatagramPacket packet = new DatagramPacket(buf, bfsze);
socket.receive(packet);
Log.i(LOG_TAG, "Packet received: " + packet.getLength());
track.write(packet.getData(), 0, bfsze);
}
// Stop playing back and release resources
socket.disconnect();
socket.close();
track.stop();
track.flush();
track.release();
speakers = false;
return;
}
catch(SocketException e) {
Log.e(LOG_TAG, "SocketException: " + e.toString());
speakers = false;
}
catch(IOException e) {
Log.e(LOG_TAG, "IOException: " + e.toString());
speakers = false;
}
}
});
receiveThread.start();
}
}
Но это всё работает только в пару с этим
public void startMic() {
// Creates the thread for capturing and transmitting audio
mic = true;
try
{
address = InetAddress.getByName("192.168.8.101");
}
catch (UnknownHostException e)
{}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// Create an instance of the AudioRecord class
Log.i(LOG_TAG, "Send thread started. Thread id: " + Thread.currentThread().getId());
AudioRecord audioRecorder = new AudioRecord (MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)*10);
int bytes_read = 0;
int bytes_sent = 0;
byte[] buf = new byte[bfsze];
try {
// Create a socket and start recording
Log.i(LOG_TAG, "Packet destination: " + address.toString());
DatagramSocket socket = new DatagramSocket();
//socket.setBroadcast(true);
audioRecorder.startRecording();
while(mic) {
// Capture audio from the mic and transmit it
bytes_read = audioRecorder.read(buf, 0, bfsze);
DatagramPacket packet = new DatagramPacket(buf, bytes_read, address, port);
socket.send(packet);
bytes_sent += bytes_read;
Log.i(LOG_TAG, "Total bytes sent: " + bytes_sent);
Thread.sleep(20, 0);
}
// Stop recording and release resources
audioRecorder.stop();
audioRecorder.release();
socket.disconnect();
socket.close();
mic = false;
return;
}
catch(InterruptedException e) {
Log.e(LOG_TAG, "InterruptedException: " + e.toString());
mic = false;
}
catch(SocketException e) {
Log.e(LOG_TAG, "SocketException: " + e.toString());
mic = false;
}
catch(UnknownHostException e) {
Log.e(LOG_TAG, "UnknownHostException: " + e.toString());
mic = false;
}
catch(IOException e) {
Log.e(LOG_TAG, "IOException: " + e.toString());
mic = false;
}
}
});
thread.start();
}
Звук который записала программа и передала такой же но на другом устройстве проигрывается нормально.
Я пробовал
ffmpeg -y -i test.mp3 -acodec pcm_s16le -f s16le -ac 2 -ar 44100 udp://192.168.8.101:8079
Но на выходе какая то инопланетянская музыка
Пробовал с помощью Python
with open("test.pcm","rb") as f:
b=f.read(14144) #14144 это размер буфера
while 1:
sock.send(b,(192.168.8.101,8079))
b=f.read(14144)
Это немножко помогло сначала норм а после всё в кучу
Кто знает в чем проблема??