Java:.mp3音频文件从服务器流式传输到客户端导致不支持的DaudioFileException
因此,我试图将Java:.mp3音频文件从服务器流式传输到客户端导致不支持的DaudioFileException,java,file,sockets,audio,streaming,Java,File,Sockets,Audio,Streaming,因此,我试图将.mp3文件从服务器实时传输到客户端(如spotify)。当我运行客户端时,我在线程“main”javax.sound.sampled.unsupportdaudiofileexception中得到了异常:无法从输入流中获得音频输入流,我看到了许多类似的帖子,但我不知道是什么错了,我被困了好几天。我试图从名为AllDay.mp3的服务器接收的示例文件是3.8MB,如果这有帮助的话 客户 import java.io.*; import java.net.*; import java
.mp3
文件从服务器实时传输到客户端(如spotify)。当我运行客户端时,我在线程“main”javax.sound.sampled.unsupportdaudiofileexception中得到了异常:无法从输入流中获得音频输入流,我看到了许多类似的帖子,但我不知道是什么错了,我被困了好几天。我试图从名为AllDay.mp3的服务器接收的示例文件是3.8MB,如果这有帮助的话
客户
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
public class AudioClient {
public static void main(String[] args) throws Exception {
// 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);
}
}
}
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();
}
}
}
服务器
import java.io.*;
import java.net.*;
public class AudioServer {
public static void main(String[] args) throws IOException {
File soundFile = new File("C:\\ServerMusicStorage\\AllDay.mp3");
System.out.println("Streaming to client : " + 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);
}
}
}
}
AudioInputStream类不支持MP3
考虑使用JavaFX媒体支持。
谢谢,我将在将来使用此功能,但现在我需要将此mp3文件实时传输到我的客户端。我花了一个月的时间寻找它。你能指导我吗?如果你不打算使用JavaFX,那么你必须在播放之前添加一个库来解码mp3文件。尝试在github上搜索术语“mp3”和“java”。例如,可能对你有用。还有javazoom/jlayer,我认为它甚至存在于maven存储库中。