我想从android向java服务器发送音频。 和流从另一个Android客户端收到的音频 使用下面的代码。
for android client:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.sound.sampled.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.*;
@SpringBootApplication
public class DemoApplication {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 44100;
static ServerSocket serverSocket1;
public static void main(String[] args) {
receiveAudioFromAndroidClient();
//streamToClientFromLocalFlie();
SpringApplication.run(DemoApplication.class, args);
}
private static void receiveAudioFromAndroidClient() {
try {
DatagramSocket serverSocket = new DatagramSocket(50005);
serverSocket1 = new ServerSocket(8072);
byte[] receiveData = new byte[4096];
format = new AudioFormat(sampleRate, 16, 1, true, false);
while (status == true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
ByteArrayInputStream baiss = new ByteArrayInputStream(
receivePacket.getData());
ais = new AudioInputStream(baiss, format, receivePacket.getLength());
// A thread solve the problem of chunky audio
new Thread(new Runnable() {
@Override
public void run() {
toSpeaker(receivePacket.getData());
streamToClient(receivePacket.getData());
}
}).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void streamToClient(byte[] data) {
Thread test = new Thread(new Runnable() {
@Override
public void run() {
try {
if (serverSocket1.isBound()) {
Socket socket = serverSocket1.accept();
socket.getOutputStream().write(data);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
test.start();
}
private static void streamToClientFromLocalFlie() {
try {
File soundFile = new File("E:\\Music\\Dariush\\Kisti.mp3");
try (ServerSocket serverSocker = new ServerSocket(8072);
FileInputStream in = new FileInputStream(soundFile)) {
if (serverSocker.isBound()) {
Socket client = serverSocker.accept();
OutputStream out = client.getOutputStream();
byte buffer[] = new byte[2048];
int count;
while ((count = in.read(buffer)) != -1)
out.write(buffer, 0, count);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void toSpeaker(byte soundbytes[]) {
try {
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(format);
//FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
//volumeControl.setValue(6.0206f);
sourceDataLine.start();
sourceDataLine.open(format);
sourceDataLine.start();
//System.out.println("format? :" + sourceDataLine.getFormat());
sourceDataLine.write(soundbytes, 0, soundbytes.length);
System.out.println(soundbytes.toString());
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
}
和java服务器:
$(document).ready(function() {
resize_to_fit();
});
function resize_to_fit() {
var fontsize = $('div#outer div').css('font-size');
$('div#outer div').css('font-size', parseFloat(fontsize) - 1);
if ($('div#outer div').height() >= $('div#outer').height()) {
resize_to_fit();
}
}
我从Android客户端发送音频并在java服务器上接收它然后在服务器上播放声音,但是当我从服务器向套接字端口上的另一个Android客户端发送音频时,声音已损坏。
我的代码中可能出现什么问题?
请帮帮我
抱歉我的英语很差