gpt4 book ai didi

java - 通过 TCP 发送音频流,UnsupportedAudioFileException

转载 作者:可可西里 更新时间:2023-11-01 02:30:26 26 4
gpt4 key购买 nike

我已经成功地通过 TCP 套接字发送和读取文本和图像数据。但是我无法发送和读取音频流数据。

服务器端示例代码:

public class ServerAudio {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
ServerSocket serverSocker = new ServerSocket();
Socket client = null;
serverSocker.bind(new InetSocketAddress(6666));
if (serverSocker.isBound()) {
client = serverSocker.accept();
OutputStream out = client.getOutputStream();
while (true) {
AudioInputStream ain = testPlay("C:/Users/Public/Music/Sample Music/adios.wav");
if (ain != null) {
AudioSystem.write(ain, AudioFileFormat.Type.WAVE, out);
}
}
}
serverSocker.close();
} catch (Exception e) {
e.printStackTrace();
}
}


public static AudioInputStream testPlay(String filename) {
AudioInputStream din = null;
try {
File file = new File(filename);
AudioInputStream in = AudioSystem.getAudioInputStream(file);
System.out.println("Before :: " + in.available());

AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat =
new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED, baseFormat.getSampleRate(),
8, baseFormat.getChannels(), baseFormat.getChannels(),
baseFormat.getSampleRate(), false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
System.out.println("After :: " + din.available());
return din;
} catch (Exception e) {
// Handle exception.
e.printStackTrace();
}
return din;
}
}

客户端示例代码:

public class RDPPlayAudioBytes {
private static Socket socket;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// SocketAddress socketAddress = new InetSocketAddress("172.19.1.50", 4444);
try {
Socket socket = new Socket("172.19.0.109", 6666);
// socket.connect(socketAddress, 10000);
if (socket != null && socket.isConnected()) {
InputStream inputStream = socket.getInputStream();
// DataInputStream din=new DataInputStream(inputStream);
while (inputStream != null) {
if (inputStream.available() > 0) {
System.out.println(inputStream.available());
InputStream bufferedIn = new BufferedInputStream(inputStream);
System.out.println("********** Buffred *********" + bufferedIn.available());
AudioInputStream ais = AudioSystem.getAudioInputStream(bufferedIn);
}
}
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*
* catch (LineUnavailableException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

我在哪里得到异常

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)

我观察到服务器向客户端发送 35394 字节的数据,但在客户端我们收到 8192 字节的数据。我无法理解为什么客户端缺少字节。

请帮助我如何通过 TCP 套接字发送音频流。

最佳答案

服务器:服务器只是流式传输声音文件的字节。不涉及 AudioSytem。将声音文件作为参数传递:

java AudioServer "C:/Users/Public/Music/Sample Music/adios.wav"

AudioServer 类的代码:

import java.io.*;
import java.net.*;

public class AudioServer {
public static void main(String[] args) throws IOException {
if (args.length == 0)
throw new IllegalArgumentException("expected sound file arg");
File soundFile = AudioUtil.getSoundFile(args[0]);

System.out.println("server: " + soundFile);

try (ServerSocket serverSocker = new ServerSocket(6666);
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);
}
}

System.out.println("server: shutdown");
}
}

客户端客户端可以播放通过命令行传递的声音文件以测试它是否有效:

java AudioClient "C:/Users/Public/Music/Sample Music/adios.wav"

不带任何参数调用它连接到服务器并播放通过套接字接收的文件:

java AudioClient

代码:

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;


public class AudioClient {
public static void main(String[] args) throws Exception {
if (args.length > 0) {
// play a file passed via the command line
File soundFile = AudioUtil.getSoundFile(args[0]);
System.out.println("Client: " + soundFile);
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(soundFile))) {
play(in);
}
}
else {
// play soundfile from server
System.out.println("Client: reading from 127.0.0.1:6666");
try (Socket socket = new Socket("127.0.0.1", 6666)) {
if (socket.isConnected()) {
InputStream in = new BufferedInputStream(socket.getInputStream());
play(in);
}
}
}

System.out.println("Client: end");
}


private static synchronized void play(final InputStream in) throws Exception {
AudioInputStream ais = AudioSystem.getAudioInputStream(in);
try (Clip clip = AudioSystem.getClip()) {
clip.open(ais);
clip.start();
Thread.sleep(100); // given clip.drain a chance to start
clip.drain();
}
}
}

AudioServer 和 AudioClient 使用的实用类:

import java.io.File;

public class AudioUtil {
public static File getSoundFile(String fileName) {
File soundFile = new File(fileName);
if (!soundFile.exists() || !soundFile.isFile())
throw new IllegalArgumentException("not a file: " + soundFile);
return soundFile;
}
}

关于java - 通过 TCP 发送音频流,UnsupportedAudioFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com