简体   繁体   中英

Stream music in loop over http using java

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?

Hello Constantine i think it's important to understand the difference between progressive download and streaming here . What you are doing is not streaming at all but a progressive download, that is to say you have to download first if you want to jump to that part of the file (ex. You Tube) while in streaming that's not necessary and you can listen to it endlessly (ex. BBC Radio)

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

If you want to go on with your present code (progressive) perhaps you should just create an never ending output stream and pause every now and then to limit the download speed.

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM