Streaming music in a loop over http using java

I have a small self-employed web server capable of handling POST \ GET requests. Also, I have a handler that receives sound files and puts them in the response stream, like this:

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();
}


}

      

Now it accepts all audio files and creates one continuous stream. I would like it to play the loop endlessly, like a little radio station on the Internet. So when my server started, I would enter the url in the browser and play the audio files from the directory in a loop.

EDIT:

If my server has the bytes it needs, how can I play those bytes in a loop like in VLC Player? I mean it will only play the stream once, but how can I loop it?

+1


source to share


1 answer


Hello Constantine, I find it important to understand the difference between progressive download and streaming here . What you are doing is not streaming at all, but progressive download, meaning you have to download first if you want to go to that part of the file (like You Tube), while streaming is unnecessary and you can listen to it is infinite (e.g. BBC Radio)

I would recommend that you check out the red5 server project that you are interested in streaming.



If you want to continue with your current code (progressive), perhaps you just need to create an infinite output stream and pause from time to time to limit the download speed.

Hope this helps!

+3


source







All Articles