我正在另一台PC上实现从MIC到Java服务器的实时流传输。但是我只听到白噪声。
我已经附上了客户端程序和服务器程序
Client:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class Mic
{
public byte[] buffer;
private int port;
static AudioInputStream ais;
public static void main(String[] args)
{
TargetDataLine line;
DatagramPacket dgp;
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float rate = 44100.0f;
int channels = 2;
int sampleSize = 16;
boolean bigEndian = true;
InetAddress addr;
AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line matching " + info + " not supported.");
return;
}
try
{
line = (TargetDataLine) AudioSystem.getLine(info);
int buffsize = line.getBufferSize()/5;
buffsize += 512;
line.open(format);
line.start();
int numBytesRead;
byte[] data = new byte[buffsize];
addr = InetAddress.getByName("127.0.0.1");
DatagramSocket socket = new DatagramSocket();
while (true) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = line.read(data, 0, data.length);
// Save this chunk of data.
dgp = new DatagramPacket (data,data.length,addr,50005);
socket.send(dgp);
}
}catch (LineUnavailableException e) {
e.printStackTrace();
}catch (UnknownHostException e) {
// TODO: handle exception
} catch (SocketException e) {
// TODO: handle exception
} catch (IOException e2) {
// TODO: handle exception
}
}
}
并且服务器端没有问题。它与android客户端AudioRecord完美运行。
Server:
import java.io.ByteArrayInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class Server {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int sampleRate = 44100;
static DataLine.Info dataLineInfo;
static SourceDataLine sourceDataLine;
public static void main(String args[]) throws Exception
{
System.out.println("Server started at port:"+port);
DatagramSocket serverSocket = new DatagramSocket(port);
/**
* Formula for lag = (byte_size/sample_rate)*2
* Byte size 9728 will produce ~ 0.45 seconds of lag. Voice slightly broken.
* Byte size 1400 will produce ~ 0.06 seconds of lag. Voice extremely broken.
* Byte size 4000 will produce ~ 0.18 seconds of lag. Voice slightly more broken then 9728.
*/
byte[] receiveData = new byte[4096];
format = new AudioFormat(sampleRate, 16, 1, true, false);
dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(format);
sourceDataLine.start();
//FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
//volumeControl.setValue(1.00f);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
ByteArrayInputStream baiss = new ByteArrayInputStream(receivePacket.getData());
while (status == true)
{
serverSocket.receive(receivePacket);
ais = new AudioInputStream(baiss, format, receivePacket.getLength());
toSpeaker(receivePacket.getData());
}
sourceDataLine.drain();
sourceDataLine.close();
}
public static void toSpeaker(byte soundbytes[]) {
try
{
System.out.println("At the speaker");
sourceDataLine.write(soundbytes, 0, soundbytes.length);
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
}
因此,我用正弦波(或某种在某种意义上类似正弦波的东西)填充了麦克风,并且您的程序运行正常。
因此,我的具体更改是:
package audioclient;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import javax.sound.sampled.*;
public class Mic {
public byte[] buffer;
private int port;
static AudioInputStream ais;
public static void main(String[] args) {
TargetDataLine line;
DatagramPacket dgp;
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float rate = 44100.0f;
int channels = 2;
int sampleSize = 16;
boolean bigEndian = true;
InetAddress addr;
AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line matching " + info + " not supported.");
return;
}
try {
line = (TargetDataLine) AudioSystem.getLine(info);
//TOTALLY missed this.
int buffsize = line.getBufferSize() / 5;
buffsize += 512;
line.open(format);
line.start();
int numBytesRead;
byte[] data = new byte[buffsize];
/*
* MICK's injection: We have a buffsize of 512; it is best if the frequency
* evenly fits into this (avoid skips, bumps, and pops). Additionally, 44100 Hz,
* with two channels and two bytes per sample. That's four bytes; divide
* 512 by it, you have 128.
*
* 128 samples, 44100 per second; that's a minimum of 344 samples, or 172 Hz.
* Well within hearing range; slight skip from the uneven division. Maybe
* bump it up to 689 Hz.
*
* That's a sine wave of shorts, repeated twice for two channels, with a
* wavelength of 32 samples.
*
* Note: Changed my mind, ignore specific numbers above.
*
*/
{
final int λ = 16;
ByteBuffer buffer = ByteBuffer.allocate(λ * 2 * 8);
for(int j = 0; j < 2; j++) {
for(double i = 0.0; i < λ; i++) {
System.out.println(j + " " + i);
//once for each sample
buffer.putShort((short)(Math.sin(Math.PI * (λ/i)) * Short.MAX_VALUE));
buffer.putShort((short)(Math.sin(Math.PI * (λ/i)) * Short.MAX_VALUE));
}
}
data = buffer.array();
}
addr = InetAddress.getByName("127.0.0.1");
try(DatagramSocket socket = new DatagramSocket()) {
while (true) {
for(byte b : data) System.out.print(b + " ");
// Read the next chunk of data from the TargetDataLine.
// numBytesRead = line.read(data, 0, data.length);
for(int i = 0; i < 64; i++) {
byte b = data[i];
System.out.print(b + " ");
}
System.out.println();
// Save this chunk of data.
dgp = new DatagramPacket(data, data.length, addr, 50005);
for(int i = 0; i < 64; i++) {
byte b = dgp.getData()[i];
System.out.print(b + " ");
}
System.out.println();
socket.send(dgp);
}
}
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO: handle exception
} catch (SocketException e) {
// TODO: handle exception
} catch (IOException e2) {
// TODO: handle exception
}
}
}
显然,我将其误解为一个512字节长的片段,并破坏了正弦波,但事实是,它产生的声音恰恰是它的本意-在特定的音调下令人麻木的声音。
考虑到这一点,我不怀疑问题出在您的代码中。我要检查的第一件事是系统正在窃听音频的线路。您有连接多个麦克风吗?网络摄像头麦克风,也许吗?您可能需要使用PulseAudio音量控制之类的实用程序进行检查。如果您尚未检查麦克风的功能,也可以这样做。他们确实有寿命。
扰乱音频流中的位并不少见,也不难。但我看不到任何可以执行此操作的地方。
一种想法可能是修改程序,以尝试在本地播放声音,然后再将其发送到服务器。这样,您至少可以确定问题是麦克风前还是麦克风后。
我面临的问题,使用MediaPlayer以及原生Android音频播放器播放音频网址。这是我的网址http://live.politiafm.com:8500/politiafm.mp3 这是我的代码使用本机音频播放器Intent意图=new Intent(android.content.Intent。ACTION_VIEW);intent.setDataAndType(Uri.parse(STR
我想做一个网站,将作为一个VoIP录音机应用程序。它将从麦克风获取音频,仅将音频传输到服务器和服务器,然后服务器将处理音频重新分配到其连接的客户端。 以下是我已经尝试过的: WebRTC(据我所知,它只是点对点的) 我设置的堆栈是带有Express的NodeJS,但我对任何有帮助的包都非常开放。 就可能性而言,我知道这是可能的,因为不和谐在他们自己的博客中写道,他们明确不发送点对点数据包,因为他们
我有一个传递客户端PCM或ADPCM数据的服务器。 我最初决定使用PCM是因为我不想处理编码和解码。 我让PCM工作,但在每一个音频块之间,我听到毛刺。(有点像剪辑) 所以我想原因可能是延迟/高质量的音频和所有这些东西。 所以我决定使用ADPCM来减少数据量。我用JavaScript编写了一个adpcm到pcm解码器。这是个麻烦。我希望,由于数据计数减少了,也许这会阻止小故障(数据将赶上正在播放的
需要您的帮助,我检查音频流是否当您按下按钮或当程序启动的时刻,当您按下按钮,音频流是不可用的,然后没有发生,音乐没有播放,没有任何响应从程序没有我想让您认识到如何这样的响应尽可能做吗?
问题内容: 我目前正在尝试将直播麦克风音频从Android设备流式传输到Java程序。我首先在两个Android设备之间发送实时音频,以确认我的方法正确。在接收设备上几乎没有任何延迟地可以完美地听到音频。接下来,我将相同的音频流发送到一个小型Java程序,并验证了数据也已正确发送到此处。现在,我想要做的是对这些数据进行编码,并以某种方式在运行Java程序的服务器上对其进行回放。我宁愿在使用HTML
我正在做一个项目,我需要发送一个音频流到一个节点。js服务器。我可以使用此功能捕获麦克风声音: 如你所见,我能够捕捉音频并在网站上播放。 现在我想把那个音频流发送到一个节点。js服务器,并将其发送回其他客户端。像语音聊天,但我不想使用WebRTC,因为我需要服务器中的流。我怎样才能做到这一点?我可以用插座吗。我想这样做?在我看到的示例中,他们录制了音频,并发送了一个文件,但我需要“实时”音频。