gpt4 book ai didi

java - 如何播放从服务器发送的波形文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:29 25 4
gpt4 key购买 nike

我使用客户端和服务器方法发送一个 wave 文件。

收到文件后如何在客户端播放?

这是用于发送波形文件的服务器代码:

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

class Server {
//D:\Documents and Settings\Desktop\gradpro\test1
static final String OUTPUTFILENAME = "C:\\Documents and Settings\\Administratore\\Desktop\\gradpro\\test1\\s1.wav";
static final int PORT = 8811;

public static void main(String args[]) {

System.out.println("New server running...\n\n");

// Infinite loop, innit
while ( true ) {

try {
//Create a socket
ServerSocket srvr = new ServerSocket(PORT);
Socket skt = srvr.accept();

//Create a file output stream, and a buffered input stream
FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);
BufferedOutputStream out = new BufferedOutputStream(fos);
BufferedInputStream in = new BufferedInputStream( skt.getInputStream() );

//Read, and write the file to the socket
int i;
while ((i = in.read()) != -1) {
out.write(i);
//System.out.println(i);
System.out.println("Receiving data...");
}
out.flush();
in.close();
out.close();
skt.close();
srvr.close();
System.out.println("Transfer complete.");
}
catch(Exception e) {

System.out.print("Error! It didn't work! " + e + "\n");
}

//Sleep...
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.err.println("Interrupted");
}

} //end infinite while loop
}



}

最佳答案

查看<a href="http://java.sun.com/products/java-media/sound/doc-sampled.html" rel="noreferrer noopener nofollow">javax.sound.sampled</a> API。

关于java - 如何播放从服务器发送的波形文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/768387/

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