I have a small self-written web-server, that is capable of processing POST\GET queries. Also, I have a Handler, that receives audio files and puts them in the response stream, like that:
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();
}
}
Right now it takes all of the audio files and creates one solid stream. I would like it to play in loop infinitely, like a small radio station in the web. So when my server is running, I enter a url in the browser and it plays the audio files from the directory in loop.
EDIT:
If my server has the needed bytes, how could I play these bytes in a loop, for example in VLC Player? I mean it will play the stream just once, but how could I loop it?