我有一个小型的自写网络服务器,能够处理 POST\GET 查询。另外,我有一个处理程序,它接收音频文件并将它们放入响应流中,如下所示:
package com.skynetwork.player.server;
import ...
public class Server {
private static Logger log = Logger.getLogger(Server.class);
//Here goes the handler.
static class MyHandler implements HttpHandler {
private String testUrl = "D:\\test";
private ArrayList<File> urls = new ArrayList<File>();
private long calculateBytes(ArrayList<File> urls) throws IOException {
long bytes = 0;
for (File url : urls) {
bytes += FileUtils.readFileToByteArray(url).length;
}
return bytes;
}
public void handle(HttpExchange t) throws IOException {
File dir = new File (testUrl);
System.out.println(dir.getAbsolutePath());
if (dir.isDirectory()) {
log.info("Chosen directory:" + dir);
Iterator<File> allFiles = (FileUtils.iterateFiles(dir, new String[] {"mp3"}, true));
while (allFiles.hasNext()) {
File mp3 = (File)allFiles.next();
if (mp3.exists()) {
urls.add(mp3);
log.info("File " + mp3.getName() + " was added to playlist.");
}
}
} else {
log.info("This is not a directory, but a file you chose.");
System.exit(0);
}
t.sendResponseHeaders(200, calculateBytes(urls));
OutputStream os = t.getResponseBody();
for (File url : urls) {
os.write(FileUtils.readFileToByteArray(url));
}
os.close();
}
}
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null);
server.start();
}
}
现在它需要所有的音频文件并创建一个可靠的流。我希望它可以无限循环播放,就像网络上的小型广播电台一样。所以当我的服务器运行时,我在浏览器中输入一个 url,它会循环播放目录中的音频文件。
编辑:
如果我的服务器有所需的字节,我如何循环播放这些字节,例如在 VLC Player 中?我的意思是它只会播放一次流,但我怎么能循环呢?