简体   繁体   English

使用Google App Engine中的Java Servlet进行音频流传输

[英]Audio Streaming with Java Servlet in Google App Engine

I am trying to create a URL in my GAE Java web application that can allow streaming an audio file, but so far it only works for direct download. 我试图在我的GAE Java Web应用程序中创建一个URL,允许流式传输音频文件,但到目前为止它只适用于直接下载。 This is what my servlet looks like so far for downloading a file from the BlobStore with BlobKey sent in the "file_id" parameter: 这是我的servlet到目前为止用于从BlobStore下载文件的情况,其中BlobKey在“file_id”参数中发送:

public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws IOException {

    BlobInfoFactory bif = new BlobInfoFactory();

    BlobKey blobKey = new BlobKey(req.getParameter("file_id"));
    String fileName = bif.loadBlobInfo(blobKey).getFilename();

    res.setContentType("text/plain");
    res.setHeader("Content-Disposition", "attachment; filename=" + fileName);

    blobstoreService.serve(blobKey, res);
}

The servlet is mapped to a URL that looks like this: xxx.appspot.com/fileDownload?file_id=yyy servlet映射到如下所示的URL:xxx.appspot.com/fileDownload?file_id=yyy

How could I access this URL on the client side (browser or mobile application) for streaming the audio data instead of doing a direct download? 如何在客户端(浏览器或移动应用程序)访问此URL以传输音频数据而不是直接下载? Would I have to change the server side, the client side protocol to access it (instead of HTTP), or both? 我是否必须更改服务器端,客户端协议才能访问它(而不是HTTP),或两者兼而有之?

Thank you! 谢谢!

  1. Check which audio formats are supported by certain browsers . 检查某些浏览器支持哪些音频格式。 Your best bet would be mp3 audio. 你最好的选择是mp3音频。

  2. Use this html snippet on client side: 在客户端使用此html代码段:

    <audio controls> <source src="http://path/to/your/audio/file.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>

  3. Make sure your server serves audio files as audio/mpeg content type (also the content of your blob must be mp3 file): 确保您的服务器提供音频文件作为audio/mpeg内容类型(blob的内容也必须是mp3文件):

    res.setContentType("audio/mpeg");

  4. Remove the Content-Disposition header. 删除Content-Disposition标头。 This is used when you want browser to save a file to local disk. 当您希望浏览器将文件保存到本地磁盘时使用此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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