我正在创建一个 java http 服务器,它使用 html5 来播放音乐。
问题是,当我通过浏览器(chromium、firefox)连接到服务器时,它不会播放音频。但是,如果我选择浏览器显示字体代码,然后复制字体代码并将其粘贴到文件 .html 上,然后使用浏览器打开该文件即可。我的问题是什么?
这是我的java服务器
package br.ufla.sd.trabfinal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerHttp extends Thread {
private Socket clientSocket;
private static ServerSocket serverSocket;
private static int portNumber = 8088;
private PrintWriter out;
/**
* WebServer constructor.
*/
private ServerHttp(Socket clientSoc) {
clientSocket = clientSoc;
start();
}
public static void main(String args[]) {
serverSocket = null;
try {
serverSocket = new ServerSocket(portNumber);
System.out.println("Connection Socket Created");
try {
while (true) {
System.out.println("Waiting for Connection");
new ServerHttp(serverSocket.accept());
}
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
} catch (IOException e) {
System.err.println("Could not listen on port:" + portNumber);
System.exit(1);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
System.err.println("Could not close port: " + portNumber);
System.exit(1);
}
}
}
public void run() {
System.out.println("New Communication, Thread " + this.getId()
+ " Started");
try {
// remote is now the connected socket
System.out.println("Connection, sending data.");
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream());
// read the data sent. We basically ignore it,
// stop reading once a blank line is hit. This
// blank line signals the end of the client HTTP
// headers.
String str = ".";
while (!str.equals(""))
str = in.readLine();
// Send the response
// Send the headers
out.println("HTTP/1.1 200 OK");
//out.println("Content-Type: text/html");
//out.println("Server: Bot");
// this blank line signals the end of the headers
out.println("");
// Send the HTML page
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\">");
out.println("<title> Músicas</title>");
out.println("</head>");
out.println("<body>");
out.println("<H1>Welcome</H1>");
out.println("<audio controls>");
out.println("<source src=\"/****/Horse.wav\" type=\"audio/wav\">");
out.println("Your browser does not support the audio element.");
out.println("</audio>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
还有一件事,如果我将文件位置更改为像此网站这样的在线声音:Site sound它有效...有人知道我的问题是什么吗?
谢谢!!
最佳答案
问题是您只是给出了音频源的路径。想象一下,一旦此页面加载到浏览器上并且用户单击启动音频文件,会发生什么。
您的浏览器将再次向服务器发送请求以获取音频文件。此时,您的服务器应该接受请求并将音频文件作为输出流发送回浏览器。
外部链接有效,因为该文件存在于外部链接中。
对于您的浏览器,该文件不存在,因为您的服务器似乎没有针对浏览器发出的请求返回任何文件。
要确认这一点,您可以检查浏览器的控制台。您将收到音频文件的 404 错误。
关于java - Hmlt5 音频不适用于 java http 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20440600/