Java客户端服务器:实时流式传输音频文件
我正在制作一个基于客户端服务器的音乐应用程序,如spotify。下面是向我的客户端发送Java客户端服务器:实时流式传输音频文件,java,file,sockets,audio,streaming,Java,File,Sockets,Audio,Streaming,我正在制作一个基于客户端服务器的音乐应用程序,如spotify。下面是向我的客户端发送mp3音频文件的唯一简单代码部分。我想要的是将这个.mp3音频文件实时播放到我的客户机上 为了实现这一目标,需要改变什么 编辑:我在stackoverflow和GitHub上看过很多类似的帖子,但我找不到任何解决方案 服务器 public void send_file_to_client(String requested_file) throws IOException { FileInputS
mp3音频文件的唯一简单代码部分。我想要的是将这个.mp3音频文件实时播放到我的客户机上
为了实现这一目标,需要改变什么
编辑:我在stackoverflow和GitHub上看过很多类似的帖子,但我找不到任何解决方案
服务器
public void send_file_to_client(String requested_file) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
File FILE_TO_SEND = new File("C:\\ServerMusicStorage\\" + requested_file + ".mp3");
byte[] mybytearray = new byte[(int) FILE_TO_SEND.length()];
try {
fis = new FileInputStream(FILE_TO_SEND);
bis = new BufferedInputStream(fis);
} catch (FileNotFoundException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
OutputStream os = null;
bis.read(mybytearray, 0, mybytearray.length);
os = connsock.getOutputStream();
System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
}
客户
public static void receive_file_from_server(String requested_file) throws IOException {
File file_to_save = new File("C:\\ClientMusicStorage\\" + requested_file + ".mp3");
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
byte[] mybytearray = new byte[FILE_SIZE];
InputStream is = clientSocket.getInputStream();
fos = new FileOutputStream(file_to_save);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead
= is.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
System.out.println("File " + requested_file + ".mp3"
+ " downloaded (" + current + " bytes read)");
}
检查堆栈溢出上的此线程:@fr3ddie谢谢,我已经看到了。正如我提到的,我在stackoverflow和GitHub上看到了很多类似的帖子,但没有一篇对我有帮助。是不是PCM字节流可以工作,但压缩音频流不能工作?如果你指出引用的帖子在哪些方面没有帮助,那将非常有帮助。