Unable to seek audio file in chrome served by my own server

狂风中的少年 提交于 2019-12-08 05:48:52

问题


I am trying to make a file server in Java which can serve seekable audio files. But my served files are not seekable in Google Chrome html5 audio player. When I seek forward it does nothing (even on downloaded content), if seek too much backward it starts from beginning. If I load the file from different location (http://www.vorbis.com/music/Epoq-Lepidoptera.ogg) then it is seekable.

Here is my server code

package com.company;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.*;
import java.net.InetSocketAddress;


public class Main {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/Epoq-Lepidoptera.ogg", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }


    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange httpExchange) throws IOException {

            long contentLength;
            File file = new File("Epoq-Lepidoptera.ogg");
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            contentLength = file.length();
            System.out.println(contentLength);

            Headers headers = httpExchange.getResponseHeaders();
            headers.add("content-type", "audio/ogg");

            // ok, we are ready to send the response.
            httpExchange.sendResponseHeaders(200, contentLength);
            OutputStream os = httpExchange.getResponseBody();
            byte[] data = new byte[1000];
            while (true) {
                int bytesRead = bis.read(data);
                if (bytesRead == -1) break;
                os.write(data, 0, bytesRead);
            }

            os.close();
            System.out.println("request served");
        }
    }
}

I am using this html file to test audio.

<html>
    <body>
        <audio controls="control" preload="auto" src="http://localhost:8000/Epoq-Lepidoptera.ogg"></audio>
    </body>
</html>

But following file is seekable

<html>
    <body>
        <audio controls="control" preload="auto" src="http://www.vorbis.com/music/Epoq-Lepidoptera.ogg"></audio>
    </body>
</html>

What I am doing wrong or what should I implement in my server to make file seekable, minimum on downloaded content (showed in white bar)

This problem is only Google Chrome, not in Firefox. Firefox can seek on loaded content.

来源:https://stackoverflow.com/questions/33261640/unable-to-seek-audio-file-in-chrome-served-by-my-own-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!