我正在尝试通过WIFI从android设备接收音频数据。所示的代码是来自论坛的一个工作示例,我用它来解决我的问题,但是是用Java编写的。python中有没有类似的解决方案,可以替代java服务器?这个java代码在python中的等效代码是什么
class Server {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 44100;
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(50005);
byte[] receiveData = new byte[3548];
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());
}
}).start();
}
// serverSocket.delete();
}
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.0f);
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();
}
}
}
Python中的服务器可以使用模块socket创建
如果要创建自己的socket服务器,可以定义自己的应用程序协议。您还可以使用它来处理现有的应用程序
你必须做以下事情(全球): -将套接字绑定到端口并开始侦听。 -等待客户端接收数据
我在网上找到了下面的代码:你唯一要做的就是根据自己的情况更改设置
一旦代码运行,它总是可以接收数据
最小值:
相关问题 更多 >
编程相关推荐