I'm trying to stream out an audio/wav using HTML5 feature in the following way:

<audio type="audio/wav" src="/sound/10/audio/full" sound-id="10" version="full" controls="">
</audio>

This is working quite well on Chrome, except for the fact it's impossible to replay or reposition the currentTime attribute:

var audioElement = $('audio').get(0)
audioElement.currentTime
> 1.2479820251464844
audioElement.currentTime = 0
audioElement.currentTime
> 1.2479820251464844

I'm serving the audio file from a Grails controller, using following code:

 def audio() {
    File file = soundService.getAudio(...)
    response.setContentType('audio/wav')
    response.setContentLength (file.bytes.length)
    response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
    response.outputStream << file.newInputStream() // Performing a binary stream copy
    response.status = 206

    return false
}

It seems though Grails is giving back an HTTP response 200 instead of 206 (Partial-Content) as you can see from the following output from Chrome:

Request URL:http://localhost:8080/sound/10/audio/full
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=9395D8FFF34B7455F937190F521AA1BC
Host:localhost:8080
Range:bytes=0-3189
Referer:http://localhost:8080/cms/sound/10/edit
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko)                 Chrome/23.0.1271.97 Safari/537.11
Response Headersview source
Content-disposition:attachment;filename=full.wav
Content-Length:3190
Content-Type:audio/wav
Date:Wed, 19 Dec 2012 13:58:44 GMT
Server:Apache-Coyote/1.1

Any idea what might be wrong?

Thanks, Amit.

ADDITION:

Changing the controller logic to:

response.status = 206
response.setContentType(version.mime)
response.setContentLength (file.bytes.length)
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")
response.outputStream << file.newInputStream() // Performing a binary stream copy

Did help with returning HTTP-206(Partial-Content), however both Chrome and Firefox won't play the audio file(mentioning again Chrome did play when it got the file with a 200...)

Screenshot of Chrome's network console

with the following info of the response:

 Request URL:http://localhost:8080/sound/10/audio/full
 Request Headersview source
 Accept-Encoding:identity;q=1, *;q=0
 Range:bytes=0-
 Referer:http://localhost:8080/cms/sound/10/edit
 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
有帮助吗?

解决方案

You might want to try:

response.reset();
response.setStatus(206);
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-length", Integer.toString(length + 1));
response.setHeader("Content-range", "bytes " + start.toString() + "-" + end.toString() + "/" + Long.toString(f.size()));
response.setContentType(...);

And this type of output should only be done if the client specifically asked for a range. You can check by using:

String range = request.getHeader("range");

if range is not null, then you'll have to parse the range for the start and end byte requests. Note that you can have "0-" as a range In some cases, you'll see "0-1" as a request to see if your service knows how to handle range requests.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top