我使用客户端和服务器方法发送wave文件。
我收到后如何在客户端播放此文件?
这是用于发送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
}
}
答案 0 :(得分:1)
查看javax.sound.sampled
API。
答案 1 :(得分:0)
您可以使用AudioClip类播放WAV文件;或者,您可以使用Java Sound API。