如何通过UDP发送音频流中的Java? [英] How to send audio stream via UDP in java?
问题描述
我有一个问题,我已设置的MediaLocator为麦克风输入,然后创建播放器。
我需要抓住从麦克风的声音,连接$ C $其C到一些质量较低的流,并把它作为通过UDP数据报包。
这里的code,我发现大部分在线和它适应了我的应用程序:
I have a problem, i have set MediaLocator to microphone input, and then created Player. I need to grab that sound from the microphone, encode it to some lower quality stream, and send it as a datagram packet via UDP. Here's the code, i found most of it online and adapted it to my app:
public class AudioSender extends Thread {
private MediaLocator ml = new MediaLocator("javasound://44100");
private DatagramSocket socket;
private boolean transmitting;
private Player player;
TargetDataLine mic;
byte[] buffer;
private AudioFormat format;
private DatagramSocket datagramSocket(){
try {
return new DatagramSocket();
} catch (SocketException ex) {
return null;
}
}
private void startMic() {
try {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, 16, 2, 4, 8000.0F, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
mic = (TargetDataLine) AudioSystem.getLine(info);
mic.open(format);
mic.start();
buffer = new byte[1024];
} catch (LineUnavailableException ex) {
Logger.getLogger(AudioSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Player createPlayer() {
try {
return Manager.createRealizedPlayer(ml);
} catch (IOException ex) {
return null;
} catch (NoPlayerException ex) {
return null;
} catch (CannotRealizeException ex) {
return null;
}
}
private void send() {
try {
mic.read(buffer, 0, 1024);
DatagramPacket packet =
new DatagramPacket(
buffer, buffer.length, InetAddress.getByName(Util.getRemoteIP()), 91);
socket.send(packet);
} catch (IOException ex) {
Logger.getLogger(AudioSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run() {
player = createPlayer();
player.start();
socket = datagramSocket();
transmitting = true;
startMic();
while (transmitting) {
send();
}
}
public static void main(String[] args) {
AudioSender as = new AudioSender();
as.start();
}
}
当我运行的接收器类,仅发生的事情,就是我听到从发送方类这个球员。
而我似乎看到TargetDataLine的和播放器之间的连接。
基本上,我需要的声音播放形式,并以某种方式将其转换为字节[],所以我可以送它作为数据报。
有任何想法吗?一切都是可以接受的,只要它的工作原理:)
And only thing that happens when I run the receiver class, is me hearing this Player from the sender class. And I cant seem to see the connection between TargetDataLine and Player. Basically, I need to get the sound form player, and somehow convert it to bytes[], therefore I can sent it as datagram. Any ideas? Everything is acceptable, as long as it works :)
推荐答案
您不要在这里Player类,你想要什么使用类的 javax.sound.sampled中。至于我可以告诉播放器是播放声音,不能访问其内容。
You don't what the Player class here, you want to use the classes in javax.sound.sampled. As far as I can tell Player is for playing a sound, not accessing its contents.
我没有测试过这一点,但尝试使用.read的TargetDataLine的要创建以填补一个缓冲区,然后发送缓冲区到其他主机。
I have not tested this, but try using .read on the TargetDataLine you are creating to fill a buffer, and then sending the buffer to the other host.
这篇关于如何通过UDP发送音频流中的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!