使用Java流环路音乐通过HTTP [英] Stream music in loop over http using java

查看:160
本文介绍了使用Java流环路音乐通过HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的自我编写的Web服务器,它能够处理POST \\ GET查询。另外,我有一个处理程序,接收音频文件,并把它们放入响应流,这样的:

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


}

现在,它需要所有的音频文件,并创建一个固体流。我想它在无限循环播放,就像在网络的小电台。所以,我的服务器运行时,我在浏览器中输入一个URL,它起着从目录中的音频文件的循环。

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.

编辑:

如果我的服务器具有所需的字节,我怎么可能在一个循环播放这些字节,例如在VLC播放器?
我的意思是将播放流只有一次,但我怎么会循环呢?

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?

推荐答案

您好康斯坦丁我想了解渐进式下载和流媒体的此处
你在做什么都而是一个渐进式下载不流,就是说你有,如果你想跳转到该文件的那部分先下载文件(如你管),而在流,这不是必要的,你可以听它无休止地(例如BBC电台)

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)

我会建议您检查出的RED5服务器项目在你感兴趣的流媒体。

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

如果你想继续你的present code(逐行扫描)也许你应该建立一个永不落幕的输出流,现在每一个停顿,然后限制下载速度。

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.

我希望这有助于!

这篇关于使用Java流环路音乐通过HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆